cmp of multiple attributes (WAS: Why are tuples immutable?)

Roy Smith roy at panix.com
Thu Dec 16 16:32:17 EST 2004


>Roy Smith wrote:
>>     def __cmp__ (self, other):
>> 	# I wish there was a less verbose way to do this!
>>         if self.block < other.block:
>>             return -1
>>         if self.block > other.block:
>>             return 1
>>         if self.lot < other.lot:
>>             return -1
>>         if self.lot > other.lot:
>>             return 1
>>         return 0

Steven Bethard  <steven.bethard at gmail.com> wrote:
>Does this do what you want?
>...     def __cmp__(self, other):
>...         return (cmp(self.block, other.block) or
>...                 cmp(self.lot, other.lot))

Yes, that's exactly what I was after.  Like so many things, it's
obvious once it's pointed out to you.  Thanks!



More information about the Python-list mailing list