Confused again
Steven Taschuk
staschuk at telusplanet.net
Mon Feb 24 14:26:38 EST 2003
Quoth Duncan Smith:
[...]
> p.s. Thanks also to Steven, and is there any detailed guidance available on
> overloading operators? I've read the documentation and had a Google, but
> there's still a few things I'm not clear about. [...]
I use section 3.3 of the Language Reference, "Special method
names", which is good documentation. I don't know of a more
tutorial-style document for these questions, I'm afraid.
> [...] ie. Steven sorted out my
> immediate problem (posted the day before last), but I'm still not 100% sure
> how I got a returned value of 0 when my code would appear to return 1.
The return values of __cmp__ and __eq__ follow different
conventions because they serve different purposes. __cmp__ tests
for order, returning
-1 if x < y 0 if x == y 1 if x > y
while __eq__ merely tests for equality, returning
0 if x != y 1 if x == y
Thus a translation step is needed if you want to implement == in
terms of __cmp__. It is this translation step which changed the 1
your __cmp__ returned into a 0.
In other words: your code returned 1, but not to the code which
did the x == y test. Rather, this 1 was returned to the code in
the interpreter which implements ==; that code thought that 1 was
following the __cmp__ convention, and so translated it to the
__eq__ convention and returned the result (0) to the code which
did the x == y test.
--
Steven Taschuk staschuk at telusplanet.net
"Its force is immeasurable. Even Computer cannot determine it."
-- _Space: 1999_ episode "Black Sun"
More information about the Python-list
mailing list