Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

are user defined classes hashable?

1 view
Skip to first unread message

Alan G Isaac

unread,
Jul 19, 2009, 9:48:16 AM7/19/09
to
Are user defined classes hashable?
(The classes; *not* the instances!)

I want to use some classes as dictionary keys.
Python is not objecting,
but I'm not sure how to think about
whether this could be dangerous.
I'm inclined to guess it will be hashed by id
and this is OK.

Thanks for any insights,
Alan Isaac

Nicolas Dandrimont

unread,
Jul 19, 2009, 10:07:57 AM7/19/09
to pytho...@python.org, Alan G Isaac
* Alan G Isaac <alan....@gmail.com> [2009-07-19 13:48:16 +0000]:

> Are user defined classes hashable?
> (The classes; *not* the instances!)
>
> I want to use some classes as dictionary keys.
> Python is not objecting,
> but I'm not sure how to think about
> whether this could be dangerous.
> I'm inclined to guess it will be hashed by id
> and this is OK.

You can check for yourself:

In [1]: class Foo(object):
...: pass
...:

In [2]: foo = Foo()

In [3]: hash
hash

In [3]: hash(foo)
Out[3]: 15294992

In [4]: id(foo)
Out[4]: 15294992

So yes, by default, user-defined classes are hashable, by id. You can
override this behaviour by defining the __hash__ special method on
your object.

HTH,
--
Nicolas Dandrimont

signature.asc

Alan G Isaac

unread,
Jul 19, 2009, 10:46:12 AM7/19/09
to
> * Alan G Isaac <alan....@gmail.com> [2009-07-19 13:48:16 +0000]:
>> Are user defined classes hashable?
>> (The classes; *not* the instances!)
>> I'm inclined to guess it will be hashed by id and this is
>> OK.


On 7/19/2009 10:07 AM Nicolas Dandrimont apparently wrote:
> You can check for yourself:

> In [1]: class Foo(object):
> ...: pass
> ...:

> In [2]: foo = Foo()

> In [3]: hash(foo)
> Out[3]: 15294992

> In [4]: id(foo)
> Out[4]: 15294992

Again, my question is about the class not its instances,
but still, checking as you suggest gives the same answer.

Thanks,
Alan

Nicolas Dandrimont

unread,
Jul 19, 2009, 11:39:31 AM7/19/09
to pytho...@python.org
* Alan G Isaac <alan....@gmail.com> [2009-07-19 14:46:12 +0000]:


> Again, my question is about the class not its instances,
> but still, checking as you suggest gives the same answer.

That's what I get for answering before my coffee!

Cheers,
--
Nicolas Dandrimont

"Linux poses a real challenge for those with a taste for late-night
hacking (and/or conversations with God)."
(By Matt Welsh)

signature.asc

Hyuga

unread,
Jul 20, 2009, 11:52:39 AM7/20/09
to
On Jul 19, 11:39 am, Nicolas Dandrimont <Nicolas.Dandrim...@crans.org>
wrote:
> * Alan G Isaac <alan.is...@gmail.com> [2009-07-19 14:46:12 +0000]:

>
> > Again, my question is about the class not its instances,
> > but still, checking as you suggest gives the same answer.
>
> That's what I get for answering before my coffee!

Regardless, Nicolas's example can be applied to the class too:

>>> class Foo(object):
pass

>>> hash(Foo)
11443104
>>> id(Foo)
11443104

class objects are just objects of type 'type'.

Aahz

unread,
Jul 20, 2009, 10:53:08 PM7/20/09
to
In article <373d6c69-6965-4a88...@k6g2000yqn.googlegroups.com>,

Hyuga <hyugar...@gmail.com> wrote:
>
>Regardless, Nicolas's example can be applied to the class too:
>
>>>> class Foo(object):
> pass
>
>>>> hash(Foo)
>11443104
>>>> id(Foo)
>11443104
>
>class objects are just objects of type 'type'.

Not quite. They certainly default that way, but changing the metaclass
changes a class's type::

class M(type):
pass

class C(object):
pass

class C2(object):
__metaclass__ = M

print type(C)
print type(C2)
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

"The volume of a pizza of thickness 'a' and radius 'z' is
given by pi*z*z*a"

Hyuga

unread,
Jul 21, 2009, 2:13:35 PM7/21/09
to
On Jul 20, 10:53 pm, a...@pythoncraft.com (Aahz) wrote:
> In article <373d6c69-6965-4a88-bdd2-8028ef850...@k6g2000yqn.googlegroups.com>,

>
> Hyuga  <hyugaricd...@gmail.com> wrote:
>
> >Regardless, Nicolas's example can be applied to the class too:
>
> >>>> class Foo(object):
> >    pass
>
> >>>> hash(Foo)
> >11443104
> >>>> id(Foo)
> >11443104
>
> >class objects are just objects of type 'type'.
>
> Not quite.  They certainly default that way, but changing the metaclass
> changes a class's type::
>
> class M(type):
>     pass
>
> class C(object):
>     pass
>
> class C2(object):
>     __metaclass__ = M
>
> print type(C)
> print type(C2)

Well, okay, you got me there. But the OP wasn't asking about classes
with different metaclasses. And besides, type(type(C2)) is still
type ;)

0 new messages