Python中的“hashable”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14535730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:45:16  来源:igfitidea点击:

What does "hashable" mean in Python?

python

提问by user1755071

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

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

When they say objects are hashableor hashable objectswhat does it mean?

当他们说对象是hashable或者hashable objects是什么意思?

采纳答案by NPE

From the Python glossary:

来自Python 词汇表

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().

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

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

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

回答by naghceuz

In python it means that the object can be members of sets in order to return a index. That is, they have unique identity/ id.

在 python 中,这意味着对象可以是集合的成员以返回索引。也就是说,它们具有唯一的身份/ID。

for example, in python 3.3:

例如,在 python 3.3 中:

the data structure Lists are not hashable but the data structure Tuples are hashable.

数据结构列表不可散列,但数据结构元组是可散列的。

回答by bks4line

Let me give you a working example to understand the hashable objects in python. I am taking 2 Tuples for this example.Each value in a tuple has a unique Hash Value which never changes during its lifetime. So based on this has value, the comparison between two tuples is done. We can get the hash value of a tuple element using the Id().

让我给你一个工作示例来理解 python 中的可散列对象。我在这个例子中使用了 2 个元组。元组中的每个值都有一个唯一的哈希值,它在其生命周期中永远不会改变。所以基于this has value,两个元组之间的比较就完成了。我们可以使用 Id() 获取元组元素的哈希值。

Comparison between 2 tuplesEquivalence between 2 tuples

2个元组之间的比较2个元组之间的等价性

回答by Jay.Zhao

In my understanding according to Python glossary, when you create a instance of objects that are hashable, an unchangeable value is also calculated according to the members or values of the instance. For example, that value could then be used as a key in a dict as below:

根据我的理解,根据 Python 术语表,当您创建可散列对象的实例时,还会根据实例的成员或值计算不可更改的值。例如,该值可以用作字典中的键,如下所示:

>>> tuple_a = (1,2,3)
>>> tuple_a.__hash__()
2528502973977326415
>>> tuple_b = (2,3,4)
>>> tuple_b.__hash__()
3789705017596477050
>>> tuple_c = (1,2,3)
>>> tuple_c.__hash__()
2528502973977326415
>>> id(a) == id(c)  # a and c same object?
False
>>> a.__hash__() == c.__hash__()  # a and c same value?
True
>>> dict_a = {}
>>> dict_a[tuple_a] = 'hiahia'
>>> dict_a[tuple_c]
'hiahia'

we can find that the hash value of tuple_a and tuple_c are the same since they have the same members. When we use tuple_a as the key in dict_a, we can find that the value for dict_a[tuple_c] is the same, which means that, when they are used as the key in a dict, they return the same value because the hash values are the same. For those objects that are not hashable, the method hashis defined as None:

我们可以发现 tuple_a 和 tuple_c 的哈希值是相同的,因为它们具有相同的成员。当我们使用 tuple_a 作为 dict_a 中的键时,我们可以发现 dict_a[tuple_c] 的值是相同的,这意味着,当它们用作 dict 中的键时,它们返回相同的值,因为哈希值是相同。对于那些不可散列的对象,hash方法定义为 None:

>>> type(dict.__hash__) 
<class 'NoneType'>

I guess this hash value is calculated upon the initialization of the instance, not in a dynamic way, that's why only immutable objects are hashable. Hope this helps.

我猜这个哈希值是在实例初始化时计算的,而不是以动态方式计算的,这就是为什么只有不可变对象是可哈希的。希望这可以帮助。

回答by ojas mohril

All the answers here have good working explanation of hashable objects in python, but I believe one needs to understand the term Hashing first.

这里的所有答案都对 python 中的可散列对象有很好的解释,但我相信首先需要了解术语散列。

Hashingis a concept in computer science which is used to create high performance, pseudo random access data structures where large amount of data is to be stored and accessed quickly.

散列是计算机科学中的一个概念,用于创建高性能、伪随机访问数据结构,其中大量数据将被快速存储和访问。

For example, if you have 10,000 phone numbers, and you want to store them in an array (which is a sequential data structure that stores data in contiguous memory locations, and provides random access), but you might not have the required amount of contiguous memory locations.

例如,如果您有 10,000 个电话号码,并且您想将它们存储在一个数组中(这是一种将数据存储在连续内存位置并提供随机访问的顺序数据结构),但您可能没有所需数量的连续内存位置。

So, you can instead use an array of size 100, and use a hash function to map a set of values to same indices, and these values can be stored in a linked list. This provides a performance similar to an array.

因此,您可以改为使用大小为 100 的数组,并使用哈希函数将一组值映射到相同的索引,并且这些值可以存储在链表中。这提供了类似于数组的性能。

Now, a hash function can be as simple as dividing the number with the size of the array and taking the remainder as the index.

现在,散列函数可以像将数字除以数组的大小并将余数作为索引一样简单。

For more detail refer to https://en.wikipedia.org/wiki/Hash_function

有关更多详细信息,请参阅https://en.wikipedia.org/wiki/Hash_function

Here is another good reference: http://interactivepython.org/runestone/static/pythonds/SortSearch/Hashing.html

这是另一个很好的参考:http: //interactivepython.org/runestone/static/pythonds/SortSearch/Hashing.html

回答by user1767754

Anything that is not mutable (mutable means, likely to change) can be hashed. Besides the hash function to look for, if a class has it, by eg. dir(tuple)and looking for the __hash__method, here are some examples

任何不可变的(可变意味着,可能会改变)都可以散列。除了要查找的散列函数,如果一个类有它,例如。dir(tuple)并寻找__hash__方法,这里有一些例子

#x = hash(set([1,2])) #set unhashable
x = hash(frozenset([1,2])) #hashable
#x = hash(([1,2], [2,3])) #tuple of mutable objects, unhashable
x = hash((1,2,3)) #tuple of immutable objects, hashable
#x = hash()
#x = hash({1,2}) #list of mutable objects, unhashable
#x = hash([1,2,3]) #list of immutable objects, unhashable

List of immutable types:

不可变类型列表:

int, float, decimal, complex, bool, string, tuple, range, frozenset, bytes

List of mutable types:

可变类型列表:

list, dict, set, bytearray, user-defined classes

回答by JAY

For creating a hashing table from scratch, all the values has to set to "None" and modified once a requirement arises. Hashable objects refers to the modifiable datatypes(Dictionary,lists etc). Sets on the other hand cannot be reinitialized once assigned, so sets are non hashable. Whereas, The variant of set() -- frozenset() -- is hashable.

为了从头开始创建哈希表,所有值都必须设置为“无”,并在出现需求时进行修改。可散列对象是指可修改的数据类型(字典、列表等)。另一方面,集合一旦分配就不能重新初始化,因此集合是不可散列的。而 set() 的变体——frozenset()——是可散列的。

回答by Sidrah

Hashable = capable of being hashed.

Hashable = 能够被散列。

Ok, what is hashing? A hashing function is a function which takes an object, say a string such as “Python,” and returns a fixed-size code. For simplicity, assume the return value is an integer.

好的,什么是哈希?散列函数是一个函数,它接受一个对象,比如一个字符串,比如“Python”,并返回一个固定大小的代码。为简单起见,假设返回值是一个整数。

When I run hash(‘Python') in Python 3, I get 5952713340227947791 as the result. Different versions of Python are free to change the underlying hash function, so you will likely get a different value. The important thing is that no matter now many times I run hash(‘Python'), I'll always get the same result with the same version of Python.

当我在 Python 3 中运行 hash('Python') 时,我得到 5952713340227947791 作为结果。不同版本的 Python 可以自由更改底层哈希函数,因此您可能会得到不同的值。重要的是,无论现在我运行 hash('Python') 多少次,使用相同版本的 Python 总是得到相同的结果。

But hash(‘Java') returns 1753925553814008565. So if the object I am hashing changes, so does the result. On the other hand, if the object I am hashing does not change, then the result stays the same.

但是 hash('Java') 返回 1753925553814008565。所以如果我正在散列的对象发生变化,结果也会发生变化。另一方面,如果我正在散列的对象没有改变,那么结果保持不变。

Why does this matter?

为什么这很重要?

Well, Python dictionaries, for example, require the keys to be immutable. That is, keys must be objects which do not change. Strings are immutable in Python, as are the other basic types (int, float, bool). Tuples and frozensets are also immutable. Lists, on the other hand, are not immutable (i.e., they are mutable) because you can change them. Similarly, dicts are mutable.

嗯,例如,Python 字典要求键是不可变的。也就是说,键必须是不会改变的对象。字符串在 Python 中是不可变的,其他基本类型(int、float、bool)也是如此。元组和冻结集也是不可变的。另一方面,列表不是一成不变的(即它们是可变的),因为您可以更改它们。同样,dicts 是可变的。

So when we say something is hashable, we mean it is immutable. If I try to pass a mutable type to the hash() function, it will fail:

所以当我们说某些东西是可散列的时,我们的意思是它是不可变的。如果我尝试将可变类型传递给 hash() 函数,它将失败:

>>> hash('Python')
1687380313081734297
>>> hash('Java')
1753925553814008565
>>>
>>> hash([1, 2])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> hash({1, 2})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'set'
>>> hash({1 : 2})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'
>>>
>>> hash(frozenset({1, 2}))
-1834016341293975159
>>> hash((1, 2))
3713081631934410656

回答by Akku2403

In Python, any immutable object (such as an integer, boolean, string, tuple) is hashable, meaning its value does not change during its lifetime. This allows Python to create a unique hash value to identify it, which can be used by dictionaries to track unique keys and sets to track unique values.

在 Python 中,任何不可变对象(例如整数、布尔值、字符串、元组)都是可散列的,这意味着其值在其生命周期内不会改变。这允许 Python 创建一个唯一的哈希值来识别它,字典可以使用它来跟踪唯一键和集合来跟踪唯一值。

This is why Python requires us to use immutable datatypes for the keys in a dictionary.

这就是 Python 要求我们为字典中的键使用不可变数据类型的原因。