Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39220 > unrolled thread

Re: Making unhashable object

Started byJean-Michel Pichavant <jeanmichel@sequans.com>
First post2013-02-19 14:44 +0100
Last post2013-02-19 14:44 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  Re: Making unhashable object Jean-Michel Pichavant <jeanmichel@sequans.com> - 2013-02-19 14:44 +0100

#39220 — Re: Making unhashable object

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2013-02-19 14:44 +0100
SubjectRe: Making unhashable object
Message-ID<mailman.2023.1361281529.2939.python-list@python.org>
----- Original Message -----
> I am trying to define a class whose instances should not be hashable,
> following:
> http://docs.python.org/2/reference/datamodel.html#object.__hash__
> 
> class A:
>     def __init__(self,a):
>         self.value=a
>     __hash__=None
>     
> 
> Then:
> 
> >>> a=A(3)
> >>> hash(a)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: 'NoneType' object is not callable
> >>> hash([2])
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: unhashable type: 'list'
> 
> I would expect the same error in both case and the error is confusing
> in the first case. What's the proper way of making an object non
> hashable?
> 
> Olive

Try

class A:
  def __hash__(self):
    raise TypeError("unhashable type: '%s'" % self.__class__.__name__)

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web