A hash function, or hasher, computes a compact hash value from input data. Dictionaries and sets use these values to index keys and items efficiently. section 3.5.2 section 3.5.1
Use a hash function with these practical expectations:
- Expect consistent input to produce the same hash value, so a repeated password can be compared with its stored hash. section 3.5.2
- Allow for hash collisions, where different objects produce the same hash value; collision handling follows a specified protocol. section 3.5.2
- Use hashable objects as dictionary keys or set items; mutable dict, list, and set objects are unhashable, while int, float, str, tuple, bool, and NoneType are hashable. section 3.5.2
- Expect complicated hashers used for cybersecurity to be effectively one-way, making raw data difficult to recover from a hash value. section 3.5.2
Inspecting Python hash values
Python's built-in hash function accepts hashable values: hash("Hello World!") produces an integer, hash(100) produces 100, and hash([1, 2, 3]) raises TypeError because lists are unhashable.
Figure 3.6 The process of hashing, using dictionary keys as an example. A hash function (hasher) hashes the keys of a dictionary, producing hashes as integer values. These hash values are uniquely associated with each of the dictionary keys. Different hashers are expected to produce different hashes.
Trace how a hash function transforms dictionary keys into integer hash values associated with those keys. section 3.5.2
Hashing supports constant-magnitude lookup for sets and dictionaries, whereas list membership requires traversal whose time grows with the list. section 3.5.1















