class inheritance
Patrick Maupin
pmaupin at gmail.com
Sat Mar 13 12:44:38 EST 2010
On Mar 13, 11:37 am, Jack Diederich <jackd... at gmail.com> wrote:
> If Fraction.__add__ returns a new object but the subclass Value is
> compatible (as I would except since it is a sublcass) then just change
> all references in Franction.__add__ to be more generic, ex/
>
> class Franction():
> def __add__(self, other):
> return self.__classs__(self.denominator + other.denominator)
>
> That way if __add__ is called by an instance of a subclass it will
> return an instance of that subclass.
>
Yes, I think the OP understands that, and also understands that he
would have to do the same thing for __sub__, __div__, __rsub__,
__radd__, etc.
That's why I suggested that, instead of writing all that tedious code,
he could write code that writes the tedious code :-)
As Terence Parr of ANTLER fame asks: "Why program by hand in five
days what you can spend five years of your life automating?"
Pat
More information about the Python-list
mailing list