[Tutor] Overloading comparisons
Hugo González Monteverde
hugonz-lists at h-lab.net
Tue Nov 15 19:54:24 CET 2005
I know it is rude to reply to myself. But I found the answer and wanted
to share it.
I did the following test:
>>> class Lala:
... def __eq__(self, other):
... if self.name == other.name:
... return True
...
... def __cmp__(self, other):
... if self.time < other.time:
... return -1
... elif self.time > other.time:
... return 1
... else:
... return 0
...
>>> ins0 = Lala()
>>> ins0.time = 11
>>> ins0.name = 'myname'
>>>
>>>
>>> ins1 = Lala()
>>> ins1.time = 10
>>> ins1.name = 'myname'
>>>
>>> ins1 == ins0
True
>>>
>>> ins0<ins1
False
>>> ins1<ins0
True
>>>
>>> #See? They test correctly for greater and lesser than (time)
... #but they test equal as to time.
...
>>> #I even tried sort, it works fine.
The thing is to use the rich comparison functions.
Hugo
More information about the Tutor
mailing list