__eq__ problem with subclasses

D

Daniel Israel

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.

A sample code for clarity:

class c1(object):

def __eq__(self, op):
print "Calling
c1.__eq__("+str(type(self))+","+str(type(op))+")"
return False

class c2(c1):

pass


a1=c1()
a2=c2()

a1==a2
a2==a1

The output is:

Calling c1.__eq__(<class '__main__.c2'>,<class '__main__.c1'>)
Calling c1.__eq__(<class '__main__.c2'>,<class '__main__.c1'>)

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'.
 
D

Daniel Israel

Scott said:
This is the quickest way to make sure that an over-ridden __eq__
gets called ...

The cost of allowing the expression order to determine the call made
when no comparison override is provided would be more computation
before finally dispatching on the method. Would you want to slow down
the comparison to get the behavior you seem to want?

Well, yes, actually. But then I am trying to get my program to work. :)

Seriously, thank you, now I at least understand the rational. There is
an implementation philosophy here, I think. What is the purpose of
allowing over-rides of these operators? If it is purely to allow code
to mimic numeric types, then it is justified the demand that over-ride
code respect certain properties of these operators. (Although, for
example, * is not commutative for general matricies. So even then it is
not totally obvious what properties should be required.) But the
reality is that many programmers will use these operators for other
purposes. If the language designer's intent is to allow for this, then
there shouldn't be surprising exceptional behavior like this, and you
have to live with the cost penalty.

Personally, I think it is worth the cost to allow the programmer
flexibility. But if that is not the decision of the language designers,
then I would say this is a documentation bug. This behavior needs to be
clear to anyone reading the manual.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

adding elements to set 0
Chanelling Guido - dict subclasses 11
Need help with finding N. 1
transpose array 7
Python module import loop issue 5
newb loop problem 5
vector addition 2
Problem with KMKfw libraries 1

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top