Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'case.': 0.05; 'callable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'typeerror:': 0.09; 'def': 0.10; "'b'": 0.16; 'hashable,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subject:object': 0.16; 'type:': 0.16; 'unhashable': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'define': 0.20; 'trying': 0.21; '"",': 0.22; 'non': 0.24; 'header:User-Agent:1': 0.26; '(most': 0.27; 'header:X-Complaints-To:1': 0.28; '>>>>': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'classes': 0.30; 'error': 0.30; 'expect': 0.31; 'url:python': 0.32; 'file': 0.32; 'instances': 0.33; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'received:org': 0.36; 'url:org': 0.36; 'should': 0.36; 'subject:: ': 0.38; 'object': 0.38; 'url:docs': 0.38; 'several': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'first': 0.61; 'making': 0.64; 'confusing': 0.84; 'subject:Making': 0.84; 'url:__hash__': 0.84; 'url:datamodel': 0.84; 'url:html#object': 0.84; 'url:reference': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Making unhashable object Date: Tue, 19 Feb 2013 14:54:50 +0100 Organization: None References: <20130219143825.3077c3b8@pcolivier.chezmoi.net> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084a5bc.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361282083 news.xs4all.nl 6964 [2001:888:2000:d::a6]:45198 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39222 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 "", line 1, in > TypeError: 'NoneType' object is not callable >>>> hash([2]) > Traceback (most recent call last): > File "", line 1, in > 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 "", line 1, in TypeError: 'NoneType' object is not callable >>> class B(object): ... __hash__ = None ... >>> hash(B()) Traceback (most recent call last): File "", line 1, in TypeError: unhashable type: 'B'