Path: csiph.com!usenet.pasdenom.info!goblin2!goblin1!goblin.stu.neva.ru!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; "subject:' ": 0.07; 'immutable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'python': 0.11; 'def': 0.12; 'choose,': 0.16; 'comparison.': 0.16; 'hashable': 0.16; 'hashable,': 0.16; 'immutable,': 0.16; 'kern': 0.16; 'mutability': 0.16; 'mutable': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sake,': 0.16; 'tuple': 0.16; 'typeerror:': 0.16; 'underlying': 0.16; 'unhashable': 0.16; 'wrote:': 0.18; 'tests': 0.22; 'header :User-Agent:1': 0.23; 'interpret': 0.24; 'header:X-Complaints- To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; "doesn't": 0.30; 'robert': 0.30; "i'm": 0.30; 'agreed.': 0.31; 'comparison': 0.31; "d'aprano": 0.31; 'default,': 0.31; 'equality': 0.31; 'steven': 0.31; 'this.': 0.32; 'quite': 0.32; 'agree': 0.35; 'classes': 0.35; 'except': 0.35; 'something': 0.35; 'objects': 0.35; 'test': 0.35; 'but': 0.35; 'false': 0.36; 'should': 0.36; 'example,': 0.37; 'nov': 0.38; 'to:addr:python- list': 0.38; 'itself': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'affect': 0.61; 'new': 0.61; 'our': 0.64; 'afraid': 0.65; 'world': 0.66; 'due': 0.66; 'believe': 0.68; 'subject': 0.69; 'default': 0.69; 'different.': 0.84; 'eco': 0.84; 'terrible': 0.84; 'reasons,': 0.91; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Robert Kern Subject: Re: 'isimmutable' and 'ImmutableNester' Date: Tue, 12 Nov 2013 14:00:07 +0000 References: <1384206048.30461.46091021.634F0FCA@webmail.messagingengine.com> <52820da6$0$30003$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 213.1.240.226 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.1.0 In-Reply-To: <52820da6$0$30003$c3e8da3$5496439d@news.astraweb.com> 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: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384264822 news.xs4all.nl 15994 [2001:888:2000:d::a6]:37921 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59194 On 2013-11-12 11:14, Steven D'Aprano wrote: > On Tue, 12 Nov 2013 18:12:43 +1100, Chris Angelico wrote: > >> def isimmutable(x): >> try: >> hash(x) >> return True >> except TypeError: >> return False > > I'm afraid that doesn't test for immutability. It tests for hashability, > which is different. I am going to nitpick below for nitpicking's sake, but I agree with this. > No well-behaved mutable object can be hashable, but that's not to say > that badly-behaved mutable objects won't be hashable. That's not quite true. A well-behaved mutable may be (well-behaved) hashable as long as the allowed mutations do not affect the equality comparison. For example, in Python 2, all new classes are mutable by default, but they are also well-behaved hashable by default because their equality comparison is identity comparison. None of the mutations affect object identity, so the hash based on identity remains well-behaved. > And every immutable > object should be hashable, but that's not to say that some immutable > objects might choose, for their own reasons, not to be hashable. I would also dispute this. A tuple itself is immutable, but it may not be hashable because one of its contained objects is unhashable (whether due to mutability or something else). > So your function is subject to both false negatives and false positives. Agreed. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco