“hashable"是什么意思?在 Python 中是什么意思? [英] What does "hashable" mean in Python?

查看:116
本文介绍了“hashable"是什么意思?在 Python 中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在互联网上搜索但找不到 hashable 的含义.

当他们说对象是 hashablehashable 对象 是什么意思?

解决方案

来自 Python 词汇表

一个>:

<块引用>

如果一个对象的哈希值在其生命周期内永远不会改变(它需要一个 __hash__() 方法),并且可以与其他对象进行比较(它需要一个 __eq__()__cmp__() 方法).比较相等的可散列对象必须具有相同的散列值.

哈希能力使对象可用作字典键和集合成员,因为这些数据结构在内部使用哈希值.

Python 的所有不可变内置对象都是可散列的,而没有可变容器(例如列表或字典)是可散列的.默认情况下,作为用户定义类实例的对象是可散列的;它们都比较不相等,它们的哈希值是它们的id().

I tried searching internet but could not find the meaning of hashable.

When they say objects are hashable or hashable objects what does it mean?

解决方案

From the Python glossary:

An object is hashable if it has a hash value which never changes during its lifetime (it needs a __hash__() method), and can be compared to other objects (it needs an __eq__() or __cmp__() method). Hashable objects which compare equal must have the same hash value.

Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.

All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or dictionaries) are. Objects which are instances of user-defined classes are hashable by default; they all compare unequal, and their hash value is their id().

这篇关于“hashable"是什么意思?在 Python 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆