實習小記-python中可雜湊對象是個啥?what is hashable object in python?

來源:互聯網
上載者:User

標籤:

廢話不多說直接祭上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__這兩個方法不能隨意動,如果我麼需要改寫其中一個的話一定要仔細考慮可能的情況,以免出現問題。

 

實習小記-python中可雜湊對象是個啥?what is hashable object in python?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.