Python中“hashable”是什么意思?我试过搜索互联网,但找不到哈希的含义。当他们说对象是什么hashable或hashable objects它是什么意思?
3 回答

慕桂英4014372
TA贡献1871条经验 获得超13个赞
任何不可变的东西(可变的意思,可能会改变)都可以进行哈希处理。除了要查找的哈希函数,如果一个类有它,例如。dir(tuple)并寻找__hash__方法,这里有一些例子
#x = hash(set([1,2])) #set unhashable
x = hash(frozenset([1,2])) #hashable
#x = hash(([1,2], [2,3])) #tuple of mutable objects, unhashable
x = hash((1,2,3)) #tuple of immutable objects, hashable
#x = hash()
#x = hash({1,2}) #list of mutable objects, unhashable
#x = hash([1,2,3]) #list of immutable objects, unhashable
不可变类型列表:
int, float, decimal, complex, bool, string, tuple, range, frozenset, bytes
可变类型列表:
list, dict, set, bytearray, user-defined classes
添加回答
举报
0/150
提交
取消