What is the correct way to define __hash__?

Chris Rebert clp2 at rebertia.com
Mon Oct 12 22:16:51 EDT 2009


On Mon, Oct 12, 2009 at 7:04 PM, Steven D'Aprano
<steven at remove.this.cybersource.com.au> wrote:
> On Mon, 12 Oct 2009 15:45:30 -0500, Peng Yu wrote:
>
>>   def __cmp__(self, other):
>>     if self._a < other._a:
>>       return -1
>>     elif self._a > other._a:
>>       return 1
>>     elif self._b < other._b:
>>       return -1
>>     elif self._b > other._b:
>>       return 1
>>     else:
>>       return 0
>
> This can be simplified to:
>
> return cmp((self._a, self._b), (other._a, other._b))

Assuming you're not using Python 3.x that is.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list