python中可哈希是什么意思_实习小记-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,如果某個情況下需要這個類型的哈希值,那么程序將會報錯。具體請看下面代碼:
>>> classA:
...def __eq__(self, other):
...returnTrue
...>>> a =A()>>> importcollections>>> isinstance(a, collections.Hashable) #發現它不是可哈希對象
False>>> a.__hash__
>>> a.__hash__()
Traceback (most recent call last):
File"", line 1, in 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__ = .__hash__.
如果某個重寫了__eq__方法的對象需要保留__hash__屬性,那么我們需要在類型設置中添加該語句 __hash__ = .__hash__
請看代碼
>>> classA:
...__hash__ = object.__hash__...def __eq__(self, other):
...returnTrue
...>>> a =A()>>> a.__hash__
>>> set([a,2,3])
{<__main__.a object at>, 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__這兩個方法不能隨意動,如果我么需要改寫其中一個的話一定要仔細考慮可能的情況,以免出現問題。
總結
以上是生活随笔為你收集整理的python中可哈希是什么意思_实习小记-python中可哈希对象是个啥?what is hashable object in python?...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue 组件名 下划线_团队Vue组件规
- 下一篇: 论文笔记:MPRNet: Multi-S