I'm defining a Python object as being "immutable at any depth" iff
For example ((1, 2), (3, 4))
is immutable at any depth, whereas ((1, 2), [3, 4])
isn't (even though the latter, by virtue of being a tuple, is "nominally" immutable).
Is there a reasonable way to test whether a Python object is "immutable at any depth"?
It is relatively easy to test for the first condition (e.g. using collections.Hashable
class, and neglecting the possibility of an improperly implemented __hash__
method), but the second condition is harder to test for, because of the heterogeneity of "container" objects, and the means of iterating over their "contents"...
Thanks!
There are no general tests for immutability. An object is immutable only if none of its methods can mutate the underlying data.
More likely, you're interested in hashability which usually depends on immutability. Containers that are hashable will recursively hash their contents (i.e. tuples and frozensets). So, your test amounts to running hash(obj)
and if it succeeds then it was deeply hashable.
IOW, your code already used the best test available:
>>> a = ((1, 2), (3, 4))
>>> b = ((1, 2), [3, 4])
>>> hash(a)
5879964472677921951
>>> hash(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
I imagine you're looking for something like this:
def deeply_hashable(obj):
try:
hash(obj)
except TypeError:
return False
try:
iter(obj)
except TypeError:
return True
return all(deeply_hashable(o) for o in obj)
One obvious problem here is that iterating over a dict
iterates over its keys, which are always immutable, rather than its values, which is what you're interested in. There is no easy way around this, aside of course from special-casing dict
-- which doesn't help with other classes that might behave similarly but are not derived from dict
At the end, I agree with delnan: there's no simple, elegant, general way to do this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With