Curious about whether we can use hash() to check if an object is mutable or not?
1 Answer
>>> from collections.abc import Hashable
>>> mutable = [list, bytearray, set, dict]
>>> immutable = [int, float, complex, str, tuple, frozenset, bytes]
>>> all(isinstance(x(), Hashable) for x in immutable)
True
>>> any(isinstance(x(), Hashable) for x in mutable)
False
all mutable objects are unhashable.
__hash__. Strictly speaking, it's possible. The requirement is that any properties involved in equality are immutable. Properties not involved in equality could be mutable. This is, however, extremely ill-advised. Why do you need to know if a particular object is mutable?