Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #39217 > unrolled thread
| Started by | Olive <diolu.remove_this_part@bigfoot.com> |
|---|---|
| First post | 2013-02-19 14:38 +0100 |
| Last post | 2013-02-19 14:54 +0100 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
Making unhashable object Olive <diolu.remove_this_part@bigfoot.com> - 2013-02-19 14:38 +0100
Re: Making unhashable object Chris Angelico <rosuav@gmail.com> - 2013-02-20 00:46 +1100
Re: Making unhashable object Peter Otten <__peter__@web.de> - 2013-02-19 14:54 +0100
| From | Olive <diolu.remove_this_part@bigfoot.com> |
|---|---|
| Date | 2013-02-19 14:38 +0100 |
| Subject | Making unhashable object |
| Message-ID | <20130219143825.3077c3b8@pcolivier.chezmoi.net> |
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
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-02-20 00:46 +1100 |
| Message-ID | <mailman.2024.1361281906.2939.python-list@python.org> |
| In reply to | #39217 |
On Wed, Feb 20, 2013 at 12:38 AM, Olive
<diolu.remove_this_part@bigfoot.com> wrote:
> 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
This is an old-style class. If you subclass object, it works as you expect:
>>> class A(object):
def __init__(self,a):
self.value=a
__hash__=None
>>> a=A(3)
>>> hash(a)
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
hash(a)
TypeError: unhashable type: 'A'
This is with Python 2.6. With Python 3 and later, that distinction no
longer exists.
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-02-19 14:54 +0100 |
| Message-ID | <mailman.2025.1361282083.2939.python-list@python.org> |
| In reply to | #39217 |
Olive wrote: > 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 Deriving your classes from object has several advantages, among them: >>> class A: ... __hash__ = None ... >>> hash(A()) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'NoneType' object is not callable >>> class B(object): ... __hash__ = None ... >>> hash(B()) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'B'
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web