Hashability questions

Discussion in 'Python' started by Bob Grommes, May 13, 2012.

  1. Bob Grommes

    Bob Grommes Guest

    Noob alert: writing my first Python class library.

    I have a straightforward class called Utility that lives in Utility.py.

    I'm trying to get a handle on best practices for fleshing out a library. As such, I've done the following for starters:

    def __str__(self):
    return str(type(self))

    # def __eq__(self,other):
    # return hash(self) == hash(other)

    The commented-out method is what I'm questioning. As-is, I can do the following from my test harness:

    u = Utility()
    print(str(u))
    print(hash(u))
    u2 = Utility()
    print(hash(u2))
    print(hash(u) == hash(u2))

    However if I uncomment the above _eq_() implementation, I get the following output:

    <class 'Utility.Utility'>
    Traceback (most recent call last):
    File "/Users/bob/PycharmProjects/BGC/Tests.py", line 7, in <module>
    print(hash(u))
    TypeError: unhashable type: 'Utility'

    Process finished with exit code 1

    Obviously there is some sort of default implementation of __hash__() at work and my implementation of _eq_() has somehow broken it. Can anyone explain what's going on?
     
    Bob Grommes, May 13, 2012
    #1
    1. Advertising

  2. Bob Grommes

    alex23 Guest

    On May 14, 5:11 am, Bob Grommes <> wrote:
    > Obviously there is some sort of default implementation of __hash__()
    > at work and my implementation of _eq_() has somehow broken it.
    > Can anyone explain what's going on?


    It looks like this has changed between Python 2 and 3:

    "If a class does not define an __eq__() method it should not define a
    __hash__() operation either; if it defines __eq__() but not
    __hash__(), its instances will not be usable as items in hashable
    collections."

    From: http://docs.python.org/dev/reference/datamodel.html#object.__hash__

    You should just be able to add a __hash__ to Utility and it'll be fine.
     
    alex23, May 15, 2012
    #2
    1. Advertising

  3. Bob Grommes

    Bob Grommes Guest

    On Monday, May 14, 2012 8:35:36 PM UTC-5, alex23 wrote:
    > It looks like this has changed between Python 2 and 3:
    >
    > "If a class does not define an __eq__() method it should not define a
    > __hash__() operation either; if it defines __eq__() but not
    > __hash__(), its instances will not be usable as items in hashable
    > collections."
    >
    > From: http://docs.python.org/dev/reference/datamodel.html#object.__hash__
    >
    > You should just be able to add a __hash__ to Utility and it'll be fine.


    Thanks, Alex. I should have mentioned I was using Python 3. I guess all this is a bit over-thought to just crank out some code -- in practice, comparing two classes for equality is mostly YAGNI -- but it's my way of coming to a reasonably in-depth understanding of how things work ...
     
    Bob Grommes, May 15, 2012
    #3
    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. =?iso-8859-1?Q?Fran=E7ois?= Pinard

    Hashability of classes, old and new

    =?iso-8859-1?Q?Fran=E7ois?= Pinard, Feb 16, 2004, in forum: Python
    Replies:
    1
    Views:
    293
    Josiah Carlson
    Feb 16, 2004
  2. Chris S.

    Hashability?

    Chris S., Jul 19, 2004, in forum: Python
    Replies:
    5
    Views:
    439
    Chris S.
    Jul 19, 2004
  3. Ali Syed
    Replies:
    3
    Views:
    582
    Mark McIntyre
    Oct 13, 2004
  4. James Stroud

    hashability

    James Stroud, Aug 12, 2009, in forum: Python
    Replies:
    20
    Views:
    707
    Carl Banks
    Aug 12, 2009
  5. kj
    Replies:
    4
    Views:
    433
    Arnaud Delobelle
    Oct 7, 2010
Loading...

Share This Page