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.

一个对象的哈希性是指在生命周期内不可改变(内置__hash__()方法),并且可以和其他对象比较(内置__eq__()方法),当这两个对象相等的时候具有相同的哈希值。

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

具有哈希性的对象才可作为字典的key和set的成员。

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().

所有不可改变的内置对象都是哈希的,可变的集合不具有哈希性(list、dictionary)用户自定义类的实例默认具有哈希性,而他们的哈希值重他们id性质中来。

 

转载于:https://my.oschina.net/avenger222/blog/3004754

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值