What is the correct way to define __hash__?

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Mon Oct 12 22:04:20 EDT 2009


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))



-- 
Steven



More information about the Python-list mailing list