您的位置:首页 > 移动开发 > Objective-C

Python 内建函数 - hash(object)

2017-03-16 09:19 609 查看
Manual

直译

实例

拓展阅读

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 CSDN:
def foobar(self):
print('Hello CSDNer!')
>>> hash(CSDN)
4561779

>>> class CSDN:
def __init(self):
self.name = 'CSDNer'
self.nick = 'Fans'
self.color = 'Red'
def __hash__(self):
return hash((self.name, self.nick, self.color))
def foobar(self):
print('Hello CSDNer!')
>>> hash(CSDN)
4561807


拓展阅读

__hash__()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python