What does “hashable” mean in Python?

后端 未结 9 780
挽巷
挽巷 2020-11-22 16:45

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

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

相关标签:
9条回答
  • 2020-11-22 17:42

    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.

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

    0 讨论(0)
  • 2020-11-22 17:44

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

    Comparison between 2 tuplesEquivalence between 2 tuples

    0 讨论(0)
  • 2020-11-22 17:48

    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.

    0 讨论(0)
提交回复
热议问题