好奇我们是否可以使用 hash() 来检查对象是否可变?
1 回答
4
>>> 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
所有可变对象都是不可散列的。
于 2017-03-31T09:42:08.510 回答