Learning Python Notes (1)

Ray DalioPrinciples

Python Dictionary

From the Python Glossary :

An object is hashable if it has a hash value which never changes during its lifetime.

# Notes

# the object need __hash__() method
# the object can be compared to other onjects (__eq__() or __cmp__() method)

All of Python’s immutable built-in objects are hashable (such as string, tuple), 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().

Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally. Therefore, in dictionary, the key can be tuple or string but not list.