liveBook · Manning

Hash function

As explained in Python How-To

Hashing is the computation of a compact hash value from input data using a hash function.

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.
CH03_F06_Cui

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

How Python How-To covers it

How deep it goes
Defines hash computation, consistency, collisions, one-way behavior, and hashability, but does not detail collision-resolution algorithms.
Examples
Uses Python hash calls, password comparison, dictionary keys, set items, and list-versus-set membership timing.
What it assumes
Assumes familiarity with Python dictionaries, sets, lists, and basic exceptions while teaching hashability from its relationship to mutability.
What you can do after
Apply it via Python's hash function to inspect hash values and reason about dictionary keys and set items.
How it connects
Connects hash functions to hashable objects, hash values, hash tables, dictionaries, sets, mutable objects, and time complexity.

Also explained in

Compare how 16 books explain hash function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.