Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #59194
| From | Robert Kern <robert.kern@gmail.com> |
|---|---|
| Subject | Re: 'isimmutable' and 'ImmutableNester' |
| Date | 2013-11-12 14:00 +0000 |
| References | <CAB6+5b_w+kBT=EDqd9wRO80am+Wp2DgrEqSpVEPWkcTAVmYQtQ@mail.gmail.com> <1384206048.30461.46091021.634F0FCA@webmail.messagingengine.com> <CAB6+5b-KZE1xWqyefFicRkk4xhr9aqSWvaOhvgHJGf0s4RpexQ@mail.gmail.com> <mailman.2431.1384240366.18130.python-list@python.org> <52820da6$0$30003$c3e8da3$5496439d@news.astraweb.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2462.1384264821.18130.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Re: 'isimmutable' and 'ImmutableNester' Chris Angelico <rosuav@gmail.com> - 2013-11-12 18:12 +1100
Re: 'isimmutable' and 'ImmutableNester' Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-12 11:14 +0000
Re: 'isimmutable' and 'ImmutableNester' Robert Kern <robert.kern@gmail.com> - 2013-11-12 14:00 +0000
csiph-web