'isimmutable' and 'ImmutableNester'

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Nov 12 12:14:46 CET 2013


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.

No well-behaved mutable object can be hashable, but that's not to say 
that badly-behaved mutable objects won't be hashable. 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.

So your function is subject to both false negatives and false positives.


-- 
Steven



More information about the Python-list mailing list