程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python magic method (10):__ hash__ (self) method

編輯:Python

Python There are some magical ways to do it , They are always surrounded by double underscores , They are object-oriented Of Python Everything . They are special ways to add magic to your classes , If your object implements ( heavy load ) A magic method , Then this method will be automatically used in special cases Python The call .

function

Definition object is hash() The behavior of a function call .

Parameters

self Represents the object itself .

Return value

An integer , Represents the hash value of the object .

Example

class MyTest(object):
def __init__(self):
self.name = ' Jane '
self.age = 18
def __hash__(self):
return hash(str(self))
sample = MyTest()
print(hash(sample))

python3 in , stay set,frozenset,dict These three data structure in , All require key values key Yes. hash Of , Because to guarantee key Uniqueness .
and __hash__ It's actually returning a int value , Used to uniquely mark this object .

General explanation hash when , At the same time, we need to use eq.

A hashable set (hashed collections), The elements that need a collection implement __eq__ and __hash__, These two methods can be compared as follows :
A hash set is a collection of buckets , But only one ball can be placed in each bucket .
__hash__ The function finds the position of the bucket , Which barrel is it .
__eq__ The function is used when there is already a ball in the bucket , But here comes another ball , It claims that it should also be put in this bucket (__hash__ The function tells it where the bucket is ), The two sides are locked in a stalemate , Then we have to use __eq__ Function to determine whether the two balls are equal (equal), If the judgment is equal , Then the ball should not be put into the bucket , The hash set remains as it is .
When a hashable set (set,frozenset,dict) call hash Function time , It's time to return to one int value . The only requirement is , If two objects are judged to be equal , So their hash The values should also be equal . When two objects are compared to be equal, the members of the object are used for comparison , It is suggested that members should be brought into Yuanzu , And then get this Yuanzu's hash It's worth comparing .

When class No definition __eq__() When the method is used , Then it should not define __hash__() Method . If it defines __eq__() Method , There is no definition __hash__() Method , Then instances of this class cannot be used in hashable collections . If a class defines a mutable object ( This should mean class One of the members of is a mutable object ), And implement 了 __eq__() Method , Then this class should not implement __hash__() Method , Because the implementation of hashable objects (implement ) Key value required key Of hash The value is constant ( If an object's hash The value has changed , Then it will be put in the wrong hash In the barrel ).

The following example :

class Foo:
def __init__(self, item):
self.item = item
def __eq__(self, other):
print(' Used equal Of the object of the function id', id(self))
if isinstance(other, self.__class__):
return self.__dict__ == other.__dict__
else:
return False
def __hash__(self):
print('f' + str(self.item) + ' Used hash function ')
return hash(self.item)
f1 = Foo(1)
f2 = Foo(2)
f3 = Foo(3)
fset = set([f1, f2, f3])
print(fset)
print()
f = Foo(3)
fset.add(f)
print('f3 Of id:', id(f3))
print('f Of id:', id(f))


 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved