__eq__ and sets

Erik Max Francis max at alcyone.com
Wed Feb 26 00:33:23 CET 2003

Tim Peters wrote:

> No, it wouldn't.  If, when you compare x to y, both objects return
> NotImplemented, Python automatically falls back to comparing x and y
> by
> their memory addresses (cmp(id(x), id(y))).  The Set class definitely
> didn't
> want you to get a nonsense result from "x == 2", and, in the absence
> of a
> meaningful result, the only way to stop the comparison machinery from
> falling back to address comparison is to raise an exception.

In a toy language I've been working on (sort of a cross between Forth
and Lisp, but of course implemented in Python), the comparison operators
(eq, ne, lt, le, gt, ge) throw on incompatible types (since, even in
Python where mismatched type ordering is usually not very useful, the
actual ordering is implementation defined).  But since it's sometimes
useful to test whether two objects are equal, even in the event they are
different types, there are "safe" versions of the equality operators
available (seq and sne) which will simply return false when the objects
are incomparable.  There are, however, no "safe" versions of the
non-equality relational operators.

 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ Grub first, then ethics.
\__/ Bertolt Brecht
    Polly Wanna Cracka? / http://www.pollywannacracka.com/
 The Internet resource for interracial relationships.

More information about the Python-list mailing list