inheritance
Sean Ross
frobozz_electric at hotmail.com
Thu Jun 12 12:02:44 EDT 2003
"Duncan Smith" <buzzard at urubu.freeserve.co.uk> wrote in message
news:bca77e$aq2$1 at newsg3.svr.pol.co.uk...
> I'm in the process of subclassing some functionality of a class that was
> getting a bit too big. But a lot of the inherited functions (eg.
__mul__())
> return objects of the parent type. Initially I had the following code...
[snip]
> def __mul__(self, other):
> res = table.NumTable.__mul__(self, other)
>
> return Table(res.values, res.variables)
[snip]
> Is there any alternative to explicitly overloading each of the parent
class
> functions that return a new instance?
Well, if in the parent class' __mul__ you do something like this:
def __mul__(self, other):
# do table.NumTable specific multiplication
return self.__class__(res.values, res.variables)
Then, when your subclasses use __mul__, the return instance will be of their
class type, e.g.
table2 = table0 * table1 # where table0 and table1 are instances of class
Table
# table2 will also be an instance of class Table, and not of table.NumTable
More information about the Python-list
mailing list