python中的哈希_Python中hash的问题

在下面这个例子里:

classItem(object):def __init__(self, foo, bar):

self.foo=foo

self.bar=bardef __repr__(self):return "Item(%s, %s)" %(self.foo, self.bar)print set([Item('1', '2'), Item('1', '2')])#输出: set([Item(1, 2), Item(1, 2)])

逻辑上讲,set中的两个对象是貌似相同的,那么set中应该只有一个对象

实际上不是这样

set是根据两个元素的hash value判断这两个对象是不是相同的。元素的hash value是通过hash方法得到的(内部__hash__() magic method)。

根据文档:

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,并且instances of user-defined classes的hash value是根据他们的id得到的。这个id(ref:https://docs.python.org/3/library/functions.html#id),可以理解为对象在内存中的地址,所以例子里的输出就不奇怪了

关于__hash__()的自定义实现,文档(ref:https://docs.python.org/3/reference/datamodel.html#object.__hash__)是这么说的:

it is advised to mix together the hash values of the components of the object that also play a part in comparison of objects by packing them into a tuple and hashing the tuple.

并且举了一个例子:

def __hash__(self):return hash((self.name, self.nick, self.color))

这里再引入一个概念: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.

简单来说,hashable的对象必须实现__hash__ and __equal__两个方法

我们之前说过了hash方法怎么实现,但是仅仅实现hash方法,是不能让刚开始的例子中输出正确的结果的。原因如下(ref:https://docs.python.org/3/reference/datamodel.html#object.__hash__):

If a class does not define an __eq__() method it should not define a __hash__() operation either; if it defines __eq__() but not __hash__(), its instances will not be usable as items in hashable collections. If a class defines mutable objects and implements an __eq__() method, it should not implement __hash__(), since the implementation of hashable collections requires that a key’s hash value is immutable (if the object’s hash value changes, it will be in the wrong hash bucket).

如果定义了eq,没有定义hash,那么显然,由于hash value不同,刚开始的例子中输出结果是错误的

定义hash的同时要定义eq

之所以要定义eq,是为了处理set中有两个对象的hash value相同,这时候要怎样处理。

eq不是每次把元素放进set里都要调用的。如果某个元素和set中的已有元素的hash value都不同,那就没有调用eq的必要了。如果即使两个元素的hash value不同,也要调用eq的话,就失去了hash的意义

所以在刚开始的例子里加上:

...def __eq__(self, other):ifisinstance(other, Item):return ((self.foo == other.foo) and (self.bar ==other.bar))else:returnFalsedef __hash__(self):return hash(self.foo + " " +self.bar)

...

就ok了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值