468,301 Members | 1,487 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,301 developers. It's quick & easy.

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

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

def __add__(self,other):
if type(other) in Numerical:
return Interval(self.lower_bound+other, self.upper_bound
+other)
else:
return Interval(self.lower_bound+other.lower_bound,
self.upper_bound+other.upper_bound)

that return new objects of the same type.

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

class HalfOpen(Interval):
#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,other):
if type(other) in Numerical:
return self.__class__(self.lower_bound+other,
self.upper_bound+other)
else:
return self.__class__(self.lower_bound+other.lower_bound,
self.upper_bound+other.upper_bound)

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

Thanks in advance,
Felix
Nov 19 '08 #1
1 1805
"Felix T." <Fj******@yahoo.comwrites:
I have a class called Interval(type.ObjectType) that is supposed to
mimic closed mathematical intervals. Right now, it has a lot of
methods like this:

def __add__(self,other):
if type(other) in Numerical:
return Interval(self.lower_bound+other, self.upper_bound
+other)
else:
return Interval(self.lower_bound+other.lower_bound,
self.upper_bound+other.upper_bound)

that return new objects of the same type.

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

class HalfOpen(Interval):
#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,other):
if type(other) in Numerical:
return self.__class__(self.lower_bound+other,
self.upper_bound+other)
else:
return self.__class__(self.lower_bound+other.lower_bound,
self.upper_bound+other.upper_bound)

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=True, closed_right=True):
...

--
Arnaud
Nov 19 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

99 posts views Thread by David MacQuigg | last post: by
reply views Thread by Paul Lyons | last post: by
19 posts views Thread by jan.loucka | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.