Manual
Return the hash value of the object (if it has one). Hash values are integers. They are used to quickly compare dictionary keys during a dictionary lookup. Numeric values that compare equal have the same hash value (even if they are of different types, as is the case for 1 and 1.0).
Note For object’s with custom __hash__() methods, note that hash() truncates the return value based on the bit width of the host machine. See __hash__() for details.
直译
返回object的哈希值(如果它有一个的话)。哈希值是整数,它们用于在字典查找时快速匹配字典键。相等的数字值具有同样的哈希值(即便它们属于不同的类型,例如1和1.0)
注意:对象自定义的__hash__()方法,hash() 基于主机的位宽来截断返回值,详情见__hash__()。
实例
>>> class 优快云:
def foobar(self):
print('Hello 优快云er!')
>>> hash(优快云)
4561779
>>> class 优快云:
def __init(self):
self.name = '优快云er'
self.nick = 'Fans'
self.color = 'Red'
def __hash__(self):
return hash((self.name, self.nick, self.color))
def foobar(self):
print('Hello 优快云er!')
>>> hash(优快云)
4561807