[Python-Dev] Getting values stored inside sets
Nick Coghlan
ncoghlan at gmail.com
Sat Apr 4 03:54:51 CEST 2009
Paul Moore wrote:
> 2009年4月3日 R. David Murray <rdmurray at bitdance.com>:
>> a == b
>>>> So, python calls a.__eq__(b)
>>>> Now, that function does:
>>>> a.key == b
>>>> Since b is an object with an __eq__ method, python calls
>> b.__eq__(a.key).
>> That's the bit I can't actually find documented anywhere.
It doesn't quite work the way RDM desribed it - he missed a step.
a == b
So, python calls a.__eq__(b)
Now, that function does:
a.key == b
which first calls a.key.__eq__(b) # This step was missing
Since str has no idea what an Element is, that returns NotImplemented.
Since __eq__ is defined as being commutative, the interpreter then tries
b.__eq__(a.key).
That function does:
b.key == a.key
which calls b.key.__eq__(a.key)
which is a well defined string comparison and returns the expected answer.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
More information about the Python-Dev
mailing list