Designing superclasses so inherited methods return objects with sametype as the instance.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Felix T.

    #1

    Designing superclasses so inherited methods return objects with sametype as the instance.

    I have a class called Interval(type.O bjectType) that is supposed to
    mimic closed mathematical intervals. Right now, it has a lot of
    methods like this:

    def __add__(self,ot her):
    if type(other) in Numerical:
    return Interval(self.l ower_bound+othe r, self.upper_boun d
    +other)
    else:
    return Interval(self.l ower_bound+othe r.lower_bound,
    self.upper_boun d+other.upper_b ound)

    that return new objects of the same type.

    The problem is that if this method is called by a subclass like

    class HalfOpen(Interv al):
    #new comparison methods
    ...
    it returns an object with Interval (not HalfOpen) type.


    I either have to redefine methods like __add__ so that they return
    objects of the right type (even though the logic is the same) or find
    some way to redefine Interval's methods so they are more flexible.
    Right now, I am looking at:

    def __add__(self,ot her):
    if type(other) in Numerical:
    return self.__class__( self.lower_boun d+other,
    self.upper_boun d+other)
    else:
    return self.__class__( self.lower_boun d+other.lower_b ound,
    self.upper_boun d+other.upper_b ound)

    Is there a standard way to do this, or a better one?

    Thanks in advance,
    Felix
  • Arnaud Delobelle

    #2
    Re: Designing superclasses so inherited methods return objects with same type as the instance.

    "Felix T." <Fjt72701@yahoo .comwrites:
    I have a class called Interval(type.O bjectType) that is supposed to
    mimic closed mathematical intervals. Right now, it has a lot of
    methods like this:
    >
    def __add__(self,ot her):
    if type(other) in Numerical:
    return Interval(self.l ower_bound+othe r, self.upper_boun d
    +other)
    else:
    return Interval(self.l ower_bound+othe r.lower_bound,
    self.upper_boun d+other.upper_b ound)
    >
    that return new objects of the same type.
    >
    The problem is that if this method is called by a subclass like
    >
    class HalfOpen(Interv al):
    #new comparison methods
    ...
    it returns an object with Interval (not HalfOpen) type.
    >
    >
    I either have to redefine methods like __add__ so that they return
    objects of the right type (even though the logic is the same) or find
    some way to redefine Interval's methods so they are more flexible.
    Right now, I am looking at:
    >
    def __add__(self,ot her):
    if type(other) in Numerical:
    return self.__class__( self.lower_boun d+other,
    self.upper_boun d+other)
    else:
    return self.__class__( self.lower_boun d+other.lower_b ound,
    self.upper_boun d+other.upper_b ound)
    >
    Is there a standard way to do this, or a better one?
    You can use type(self) rather than self.__class__

    But I wouldn't make HalfOpen descend from Interval as a half-open
    interval is not a kind of closed interval. Moreover you should have

    Interval(2, 3) + HalfOpen(5, 7) == HalfOpen(7, 10)

    Your implementation will yield Interval(7, 10) if I understand
    correctly.

    I think if I implemented an Interval class I would have it something
    like this:

    class Interval:
    def __init__(self, left, right, closed_left=Tru e, closed_right=Tr ue):
    ...

    --
    Arnaud

    Comment

    Working...