What does hash do in python?

前端 未结 5 756
春和景丽
春和景丽 2020-11-28 01:31

I saw an example of code that where hash function is applied to a tuple. As a result it returns a negative integer. I wonder what does this function do? Google

5条回答
  •  心在旅途
    2020-11-28 02:03

    The Python docs for hash() state:

    Hash values are integers. They are used to quickly compare dictionary keys during a dictionary lookup.

    Python dictionaries are implemented as hash tables. So any time you use a dictionary, hash() is called on the keys that you pass in for assignment, or look-up.

    Additionally, the docs for the dict type state:

    Values that are not hashable, that is, values containing lists, dictionaries or other mutable types (that are compared by value rather than by object identity) may not be used as keys.

提交回复
热议问题