The Python docs clearly state that x==y
calls x.__eq__(y)
. However it seems that under many circumstances, the opposite is true. Where is it docu
Is this not documented in the Language Reference? Just from a quick look there, it looks like __cmp__
is ignored when __eq__
, __lt__
, etc are defined. I'm understanding that to include the case where __eq__
is defined on a parent class. str.__eq__
is already defined so __cmp__
on its subclasses will be ignored. object.__eq__
etc are not defined so __cmp__
on its subclasses will be honored.
In response to the clarified question:
I know that
__eq__
is called in preferecne to__cmp__
, but I'm not clear whyy.__eq__(x)
is called in preference tox.__eq__(y)
, when the latter is what the docs state will happen.
Docs say x.__eq__(y)
will be called first, but it has the option to return NotImplemented
in which case y.__eq__(x)
is called. I'm not sure why you're confident something different is going on here.
Which case are you specifically puzzled about? I'm understanding you just to be puzzled about the "b" == tsc
and tsc == "b"
cases, correct? In either case, str.__eq__(onething, otherthing)
is being called. Since you don't override the __eq__
method in TestStrCmp, eventually you're just relying on the base string method and it's saying the objects aren't equal.
Without knowing the implementation details of str.__eq__
, I don't know whether ("b").__eq__(tsc)
will return NotImplemented
and give tsc a chance to handle the equality test. But even if it did, the way you have TestStrCmp defined, you're still going to get a false result.
So it's not clear what you're seeing here that's unexpected.
Perhaps what's happening is that Python is preferring __eq__
to __cmp__
if it's defined on either of the objects being compared, whereas you were expecting __cmp__
on the leftmost object to have priority over __eq__
on the righthand object. Is that it?