class inheritance

Patrick Maupin pmaupin at gmail.com
Sat Mar 13 12:09:20 EST 2010


On Mar 13, 11:03 am, JLundell <jlund... at 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?

7 years ago, I had a similar problem for a different and now obsolete
reason.  I'm sure my solution could be easily updated though.  I wrote
code to write a wrapper class.  Sort of a meta-module.  Original
reference here:

http://groups.google.com/group/comp.lang.python/browse_frm/thread/1253bbab7dfd4b/59289c16603fb374?hl=en&lnk=gst&q=pmaupin+userint#59289c16603fb374

HTH,
Pat



More information about the Python-list mailing list