国际访客建议访问 Primers 编程伙伴 国际版站点 > Python 教程 > hash 以获得更好的体验。

# Python 的内置函数 hash

请查看 Python 内建函数列表 了解更多相关 API。

说明:获取哈希值。

# 函数说明

def hash(obj):
    '''
    获取哈希值

    :param obj: 一个对象
    :return: 对象的哈希值
    '''

!subtitle:说明

通过调用对象的 __hash__ 方法,获取对象的哈希值。这个值被用作 dictsetfrozenset 的唯一键。

!subtitle:参数

  • obj - 要获取哈希值的对象

!subtitle:返回值

对象的哈希值。

# 示例

运行示例

print(hash(0))
print(hash(233))
print(hash(3.1415926))
print(hash(''))
print(hash('hello world'))

# 推荐阅读

本文 更新于: 2025-11-27 09:37:59 创建于: 2025-11-27 09:37:59