On Sat, Mar 13, 2010 at 12:03 PM, JLundell <jlund...@pobox.com> wrote: > I've got a subclass of fractions.Fraction called Value; it's a mostly > trivial class, except that it overrides __eq__ to mean 'nearly equal'. > However, since Fraction's operations result in a Fraction, not a > Value, I end up with stuff like this: > > x = Value(1) + Value(2) > > where x is now a Fraction, not a Value, and x == y uses > Fraction.__eq__ rather than Value.__eq__. > > This appears to be standard Python behavior (int does the same thing). > I've worked around it by overriding __add__, etc, with functions that > invoke Fraction but coerce the result. But that's tedious; there are a > lot of methods to override. > > So I'm wondering: is there a more efficient way to accomplish what I'm > after?
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. -Jack -- http://mail.python.org/mailman/listinfo/python-list