python中不可变对象都是可哈希的吗,Python | 术语:Hashable

An object is hashable if it has a hash value which never changes during its lifetime(it needs a __hash__()method), and can be compared to other objects(it needs an __eq__() method). Hashable objects which compare equal must have the same hash value.

Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.

All of Python's immutable built-in objects are hashable; mutable containers (such as lists or dictionaries) are not. Objects which are instances of user-defined classes are hashable by default. They all compare unequal

(except with themselves), and their hash value is derived from their id().

如果一个对象在其生命周期内,有一个固定不变的哈希值(__hash__()方法)并且可以跟其他对象进行比较(__eq__()方法),那么该对象是可哈希的。比较结果相等的可哈希对象必须有相同的哈希值。

散列性(哈希)使对象可用作字典键值和集合元素,因为这些数据结构在内部使用散列值。

所有python内置的不可变对象都是可哈希的;同时,可变容器都是不可哈希的。默认情况下,用户自定义类的实例对象是可哈希的,这些实例对象各不相同,并且各自的哈希值都由id()方法派生得出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值