473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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 1961
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
9
by: Alex | last post by:
I have a serious problem and I hope there is some solution. It is easier to illustrate with a simple code: >>> class Parent(object): __slots__= def __init__(self, a, b): self.A=a; self.B=b...
6
by: Gary James | last post by:
This may not be a direct C# question, but since I'll be using using C# for development, I thought I'd pose the question here. I'll soon be involved in the design of a new software product that...
9
by: Codex Twin | last post by:
Hi I have a common model for a Data Access Layer scenario. I have an abstract base class, called DalBase which contains a list of abstract methods. Lets call them: public abstract void Shine();...
0
by: Paul Lyons | last post by:
Hi, I've been having an issue using reflection and custom attributes to create objects on the fly. I was wondering if this was expected behaviour or a known bug? When using Type.GetMembers()...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
1
weaknessforcats
by: weaknessforcats | last post by:
Introduction Polymorphism is the official term for Object-Oriented Programming (OOP). Polymorphism is implemented in C++ by virtual functions. This article uses a simple example hierarchy which...
8
by: Fuzzyman | last post by:
Hello all, I may well be being dumb (it has happened before), but I'm struggling to fix some code breakage with Python 2.6. I have some code that looks for the '__lt__' method on a class: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.