What is the correct way to define __hash__?

Discussion in 'Python' started by Peng Yu, Oct 12, 2009.

  1. Peng Yu

    Peng Yu Guest

    Hi,

    I'm wondering what is the general way to define __hash__. I could add
    up all the members. But I am wondering if this would cause a
    performance issue for certain classes.

    Regards,
    Peng


    #!/usr/bin/env python

    class A:
    def __init__(self, a, b) :
    self._a = a
    self._b = b

    def __str__(self):
    return 'A(%s, %s)' %(self._a, self._b)

    __repr__ = __str__

    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

    def __hash__(self):
    return self._a + self._b

    if __name__ == '__main__':

    x = A(1, 1)

    aset = set()
    aset.add(x)
    print aset
    Peng Yu, Oct 12, 2009
    #1
    1. Advertising

  2. 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
    Steven D'Aprano, Oct 13, 2009
    #2
    1. Advertising

  3. Peng Yu

    Chris Rebert Guest

    On Mon, Oct 12, 2009 at 7:04 PM, Steven D'Aprano
    <> 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
    Chris Rebert, Oct 13, 2009
    #3
  4. Peng Yu

    greg Guest

    Chris Rebert wrote:
    > On Mon, Oct 12, 2009 at 7:04 PM, Steven D'Aprano
    > <> wrote:
    >
    >>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.


    If you're using Python 3, you won't be writing a __cmp__
    method in the first place.

    --
    Greg
    greg, Oct 14, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Fleiter

    Difference of hash and __hash__()

    Stefan Fleiter, Sep 2, 2003, in forum: Python
    Replies:
    1
    Views:
    248
    Terry Reedy
    Sep 2, 2003
  2. Delaney, Timothy C (Timothy)

    RE: Difference of hash and __hash__()

    Delaney, Timothy C (Timothy), Sep 2, 2003, in forum: Python
    Replies:
    5
    Views:
    274
    Stefan Fleiter
    Sep 8, 2003
  3. Jeff Epler

    Re: Difference of hash and __hash__()

    Jeff Epler, Sep 2, 2003, in forum: Python
    Replies:
    4
    Views:
    355
    Terry Reedy
    Sep 3, 2003
  4. =?ISO-8859-1?Q?Jan-Erik_Meyer-L=FCtgens?=

    dictionary keys, __hash__, __cmp__

    =?ISO-8859-1?Q?Jan-Erik_Meyer-L=FCtgens?=, Nov 4, 2003, in forum: Python
    Replies:
    8
    Views:
    365
    Michael Hudson
    Nov 5, 2003
  5. Thomas Guettler

    tuple.__hash__()

    Thomas Guettler, Jan 6, 2004, in forum: Python
    Replies:
    4
    Views:
    326
    Thomas Guettler
    Jan 6, 2004
Loading...

Share This Page