2007/10/26, Robert Miller <[EMAIL PROTECTED]>:
> I have some questions about Python comparison. Suppose I have a Python
> class representing objects under some ordering, and I want to
> implement <=, <, >=, >, ==, != comparisons on those objects. I
> remember hearing somewhere that the __cmp__ method was deprecated, but
> now that I am thinking about it I can't find anything confirming that.
> Is there any reason to implement the functions __eq__, __neq__,
> __gt__, __ge__, __lt__, __le__, instead of just one __cmp__? Including
> forward compatibility, being more Python-ic or anything else?

I don't think __cmp__ is deprecated. From the docs
(http://docs.python.org/ref/customization.html), it looks like __le__,
__lt__, etc are called in preference to __cmp__ if they are defined.
Otherwise, __cmp__() is called. I think one main reason for using
these is to define more fine-grained relationships (partial ordering):
"""
There are no implied relationships among the comparison operators. The
truth of x==y does not imply that x!=y is false. Accordingly, when
defining __eq__(), one should also define __ne__() so that the
operators will behave as expected.
"""

So, just use __cmp__  and if you want to overload != for example, just
write your own __ne__

didier

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~----------~----~----~----~------~----~------~--~---

Reply via email to