实习小记-python中可哈希对象是个啥?what is hashable object in python?

废话不多说直接祭上python3.3x的文档:(原文链接

object.__hash__(self)

Called by built-in function hash() and for operations on members of hashed collections including set, frozenset, and dict. __hash__() should return an integer. The only required property is that objects which compare equal have the same hash value; it is advised to somehow mix together (e.g. using exclusive or) the hash values for the components of the object that also play a part in comparison of objects.

可哈希对象是对象拥有__hash__(self)内置函数的对象。对于可哈希的对象执行这个函数将会返回一个整数。可哈希对象判断相等的唯一条件就是两者 的哈希值相等。如果我们的对象有多个属性值,我们会使用一种方法(比方说逻辑运算异或)来将其属性值结合在一起做比较。(如果不对,麻烦一定告诉我,谢谢!)

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__();因为如果它定义了__eq__而没有定义__hash__,那么它的实例在某个可哈希集合中将会无效(比方说在字典/集合这类类型中)。如果一个类型定义了一个可变对象而且定义了__eq__方法,那么它不应该去定义__hash__方法,因为在哈希集合中要求其中元素的哈希值是不变的(如果某个对象实例的哈希值发生了改变,那么他将会到错误的哈希表中。。)。

User-defined classes have __eq__() and __hash__() methods by default; with them, all objects compare unequal (except with themselves) and x.__hash__() returns an appropriate value such that x == y implies both that x is y and hash(x) == hash(y).

用户定义的类中都有默认的__eq__和__hash__方法;有了它,所有的对象实例都是不等的(除非是自己和自己比较),在做 x == y 比较时是和这个等价的 hash(x) == hash(y)

A class that overrides __eq__() and does not define __hash__() will have its __hash__() implicitly set to None. When the __hash__() method of a class is None, instances of the class will raise an appropriate TypeError when a program attempts to retrieve their hash value, and will also be correctly identified as unhashable when checking isinstance(obj, collections.Hashable).

这句话的意思是,如果我们定义了一个类型,该类型只定义了__eq__而没有定义__hash__的话,那么他的__hash__()会隐式的设置为None,如果某个情况下需要这个类型的哈希值,那么程序将会报错。具体请看下面代码:

>>> class A:
...     def __eq__(self, other):
...         return True
...
>>> a = A()
>>> import collections
>>> isinstance(a, collections.Hashable)  # 发现它不是可哈希对象
False
>>> a.__hash__
>>> a.__hash__()
Traceback (most recent call last):
  File "<ipython-input-20-0fddd0562e01>", line 1, in <module>
    a.__hash__()
TypeError: ‘NoneType‘ object is not callable

然后往下看文档

If a class that overrides __eq__() needs to retain the implementation of __hash__() from a parent class, the interpreter must be told this explicitly by setting __hash__ = <ParentClass>.__hash__.

如果某个重写了__eq__方法的对象需要保留__hash__属性,那么我们需要在类型设置中添加该语句 __hash__ = <ParentClass>.__hash__

请看代码

>>> class A:
...     __hash__ = object.__hash__
...     def __eq__(self, other):
...         return True
...
>>> a = A()
>>> a.__hash__
<method-wrapper ‘__hash__‘ of A object at 0x7f21029cfa10>
>>> set([a,2,3])
{<__main__.A object at 0x7f21029cfa10>, 2, 3}

If a class that does not override __eq__() wishes to suppress hash support, it should include __hash__ = None in the class definition. A class which defines its own __hash__() that explicitly raises a TypeError would be incorrectly identified as hashable by an isinstance(obj, collections.Hashable) call.

如果某个类型定义需要将__hash__属性删掉,那么我们可以在类变量中这样写 __hash__ = None

看完了还是有点小激动的,今天因为一个偶然原因,接触到了这么多的python知识。真的是相当高兴阿!

然而我也发现__eq__ __hash__这两个方法不能随意动,如果我么需要改写其中一个的话一定要仔细考虑可能的情况,以免出现问题。

时间: 2024-08-01 07:28:56

实习小记-python中可哈希对象是个啥?what is hashable object in python?的相关文章

实习小记-python中不可哈希对象设置为可哈希对象

在这篇之前,我又专门翻译过官方python3.3的可哈希对象文档,大家可以先参考一下: 实习小记-python中可哈希对象是个啥?what is hashable object in python? 预备知识: 在定义一个类时,如果我们需要改写该类的__eq__函数,特别要注意的是它将会变为不可哈希对象,也就是说如果你将它放到哈希集会报错误 >>> class A: ... def __init__(self, x): ... self.x = x ... def __eq__(self

实习小记-python 内置函数__eq__函数引发的探索

乱写__eq__会发生啥?请看代码.. >>> class A: ... def __eq__(self, other): # 不论发生什么,只要有==做比较,就返回True ... return True ... >>> a = A() >>> b = A() >>> a == b True >>> a != b # 这个地方为什么会返回False? False >>> a > b Trac

Python中的指针:有什么意义?

如果您曾经使用过C或C ++等低级语言,那么您可能已经听说过指针.指针允许您在部分代码中创建高效率.它们也会给初学者带来困惑,并且可能导致各种内存管理错误,即使对于专家也是如此.那么它们在Python中的位置,以及如何在Python中模拟指针? 为什么Python没有指针? 事实是我不知道.Python中的指针本身可以存在吗?可能,但指针似乎违背了Python的禅宗.指针鼓励隐含的变化而不是明确的变化.通常,它们很复杂而不是简单,特别是对于初学者.更糟糕的是,他们乞求用脚射击自己的方法,或做一些

使用C语言为python编写动态模块(1)--从底层深度解析python中的对象以及变量

楔子 我们知道可以通过使用C语言编写动态链接库的方式来给python加速,但是方式是通过ctypes来加载,通过类CDLL将动态链接库加载进来得到一个对象之后,通过这个对象来调用动态链接库里面的函数.那么问题来了,我们可不可以使用C语言为python编写模块呢?然后在使用的时候不使用ctypes加载动态库的方式,而是通过python的关键字import进行加载. 答案是可以的,我们知道可以通过编写py文件的方式来得到一个模块,那么也可以使用C语言来编写C源文件,然后再通过python解释器进行编

python中yield用法

在介绍yield前有必要先说明下Python中的迭代器(iterator)和生成器(constructor). 一.迭代器(iterator) 在Python中,for循环可以用于Python中的任何类型,包括列表.元祖等等,实际上,for循环可用于任何“可迭代对象”,这其实就是迭代器 迭代器是一个实现了迭代器协议的对象,Python中的迭代器协议就是有next方法的对象会前进到下一结果,而在一系列结果的末尾是,则会引发 StopIteration.任何这类的对象在Python中都可以用for循

【转载】关于Python中的yield

在介绍yield前有必要先说明下Python中的迭代器(iterator)和生成器(constructor). 一.迭代器(iterator) 在Python中,for循环可以用于Python中的任何类型,包括列表.元祖等等,实际上,for循环可用于任何“可迭代对象”,这其实就是迭代器 迭代器是一个实现了迭代器协议的对象,Python中的迭代器协议就是有next方法的对象会前进到下一结果,而在一系列结果的末尾是,则会引发 StopIteration.任何这类的对象在Python中都可以用for循

python中json模块dumps和loads的区分

b=urllib2.urlopen("http://www.baidu.com")a=b.read()#这里读取的是html中的json格式的内容a1=json.loads(a)#把读取到的内容从json转换成Python中的字典格式print a1['消息状态']#用Python中的字典取值方法 a2=json.dumps(a1)#此时a1已经相当于Python中的字典了,不能称之为json了,此法可以把Python中的字典再次转成json

Python中变量的作用域(variable scope)

http://www.crifan.com/summary_python_variable_effective_scope/ 解释python中变量的作用域 示例: 1.代码版 1 #!/usr/bin/python 2 # -*- coding: utf-8 -*- 3 """ 4 ------------------------------------------------------------------------------- 5 Function: 6 [整理

Python中yield

在介绍yield前有必要先说明下Python中的迭代器(iterator)和生成器(Generator). 一.迭代器(iterator) 在Python中,for循环可以用于Python中的任何类型,包括列表.元祖等等,实际上,for循环可用于任何"可迭代对象",这其实就是迭代器 迭代器是一个实现了迭代器协议的对象,Python中的迭代器协议就是有next方法的对象会前进到下一结果,而在一系列结果的末尾是,则会引发 StopIteration.任何这类的对象在Python中都可以用f