overloading ==

Dave Brueck dbrueck at edgix.com
Thu Feb 1 14:26:11 EST 2001


Add a __cmp__ method to your class:

def __cmp__(self, obj):
  # return 0, a negative, or a positive number

(you can also define __rcmp__ to handle cases where the object on the left
side of the conversion doesn't have a cmp method)

Note that if you define __cmp__ but not a __hash__ then your objects can't
act as dictionary keys (which is correct if they are mutable).

-Dave

> [mailto:python-list-admin at python.org]On Behalf Of Amritansh Raghav
>
> can it be done?
> i looked at the operator documentation under Python Runtime Services - but
> didnt find anything to suggest it could be done.





More information about the Python-list mailing list