在python中有没有散列的内置对象是可变的?

2024-04-20 15:08:55 发布

您现在位置:Python中文网/ 问答频道 /正文

想知道我们是否可以使用hash()来检查一个对象是否可变?在


Tags: 对象hash
1条回答
网友
1楼 · 发布于 2024-04-20 15:08:55
>>> 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

所有可变对象都是不可损坏的。在

相关问题 更多 >