__eq__ problem with subclasses

D'Arcy J.M. Cain darcy at druid.net
Sat Aug 23 15:06:54 CEST 2008


On Fri, 22 Aug 2008 14:37:06 -0600
"Daniel Israel" <dmi1 at hushmail.com> wrote:
> I am very confused by the following behavior.
> 
> I have a base class which defines __eq__.  I then have a subclass 
> which does not.  When I evaluate the expression a==b, where a and b 
> are elements of these classes, __eq__ is always called with the 
> subclass as the first argument, regardless of the order I write my 
> expression.  I can't see why this would be desired behavior.

I am sure that Python is doing some optimization based on the fact that
"==", like "*" and "+" but unlike "/" and "-" are commutative.

> Why does a1==a2 generate a call to c1.__eq__(a2, a1) instead of 
> c1.__eq__(a1, e2)?  This is important because I am writing a math 
> library in which '==' is being used for assignment, i.e., 'a==b+c' 
> set 'a' equal to the sum of 'b' and 'c'.  So 'a==b' is very 
> different from 'b==a'.

But isn't that what "a = b + c" does?

-- 
D'Arcy J.M. Cain <darcy at druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.



More information about the Python-list mailing list