7 routes42 sections324 units

Unit 3.1 · about 4 h

10 / 324

Names, identity, mutability, copying and hashability

Almost every silent data bug is shared mutable state. This unit makes aliasing visible before frameworks add more of it.

  • explain
  • measure

You can now

  • Explain name binding, identity and equality using a novel nested-object example, and state when CPython may reuse an immutable object.
  • Predict visible mutations for aliases, shallow copies and deep copies of a nested structure.
  • State the dictionary and set key contract, including why a tuple holding a list is unhashable and why NaN lookups fail.

Covers

  • Name binding, object identity and equality
  • Mutability, aliasing and visible state changes
  • Shallow and deep copying, hashability and dictionary key contracts

Study, in this order

4 steps

  1. Shallow and deep copying
    Stas' Python Cookbook - selected path

    Lists: Copying (shallow vs deep), including the listB = listA warning

  2. Tuples, sets and dictionaries
    Stas' Python Cookbook - selected path

    Tuples, Sets and Dictionaries: construction, mutation, frozenset, key rules and insertion order

  3. Objects, values and types
    Python language reference

    Section 3.1: identity, type and value; mutable versus immutable; the a = 1; b = 1 reuse rule

  4. Default equality and hashing
    Python language reference

    object.__eq__ and object.__hash__: default identity equality; defining __eq__ without __hash__ removes hashing

Focus on

  • Assignment binds a name to an object. is tests identity and == tests value; CPython reuses small immutable objects, but the reference says reuse may or may not happen, so never branch on it.
  • Two names can own one mutable object. A shallow copy shares every nested object; only a deep copy duplicates them. Copying an immutable such as a tuple or str may return the same object.
  • A key must stay hashable and equal for its lifetime in the container. A tuple is hashable only when all its items are; NaN never equals itself, so a fresh NaN misses a stored NaN key.
  • The cookbook says tuples are hashable because they are immutable. That shortcut is wrong for tuples that hold mutables; hash((1, [2])) raises TypeError.

Reproduce closed-book

Closed-book, predict a, c and d after a = [[0]]; b = a; c = copy.copy(a); d = copy.deepcopy(a); b[0].append(1). Then quote the measured copy costs: shallow about 3 us and deep about 1.4 ms per copy of a 2000 by 10 nested list on CPython 3.14.4 default build, 12th Gen Intel Core i9-12900K, timeit.repeat best of 5.

Traps

  • Comparing values with is. Constant folding makes 257 is 257 true inside one compilation unit, and CPython warns (SyntaxWarning) on is with a literal, so is proves nothing about equality.
  • dict.fromkeys(keys, []) shares one list across all keys; appending through one key shows up under every key.
  • Reading d[float('nan')] after storing under another NaN. Lookup needs equality with the stored key, and NaN never equals NaN.

Practice

The section mini-project covers this: diagnosing and fixing the seeded aliasing defect with a regression test.

Self-test

  1. After a = [1, [2]]; b = a; c = copy.copy(a); b.append(3); b[1].append(4), what are a and c?

  2. Why does hash((1, 2)) work while hash((1, [2])) raises TypeError?

  3. d = {}; k = float('nan'); d[k] = 1. Why does d[float('nan')] raise KeyError?

  4. Give a case where x is y holds but x == y is False.

Completion criterion

Stop when

You predict the alias, copy and NaN cases without running them, and your copy-cost numbers match a fresh timeit run.

Mark passed, then open 3.2 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • Stas' Python Cookbook - selected path
    Stas Bekmanprimarycourse
    Run Part I on dictionaries, iteration, functions, closures, decorators, classes and context managers; Part II on modules, files, paths, environment and arguments; and Part IV on versions, dependencies, packaging and code quality. Finish by building one src-layout package from a blank directory.
  • Python language reference
    Python Software Foundationrequiredreference
    Check Data model, Execution model, Import system and Exceptions whenever a claim depends on Python semantics. These pages are the authority when the course and a runtime observation disagree.

Also in this section

  • Packaging Python Projects
    Python Packaging Authorityrequiredimplementation
    Follow the packaging tutorial once from a blank directory, using a src layout, pyproject.toml, an isolated build and a clean-environment installation test.
  • AlgoMaster Python - structured revision
    AlgoMastersupportpractice
    Use Functions, Modules and Packages, OOP, Advanced OOP, Exception Handling, Iterators and Generators, Type Hints and Annotations only after the cookbook pass. Open it for recall practice or a diagnosed weak mechanism, not as a second complete course.
  • dataclasses - data classes
    Python Software Foundationreferencereference
    Read the @dataclass parameters (eq, order, frozen, slots), field() with default_factory, frozen instances, post-init processing, replace, asdict and astuple. Use for the validated frozen dataclass in attributes-protocols-dataclasses.
  • typing - type hints and protocols
    Python Software Foundationreferencereference
    Read Protocols for structural typing and runtime_checkable limits, Generics for TypeVar generics, and assert_type for narrowing checks. Use for the Protocol and narrowing parts of type-hints-protocols; PEP 695 syntax itself is in the language reference.
  • gc - cycle garbage collector
    Python Software Foundationreferencereference
    Read the cycle collector interface: collect, get_count, garbage and disable/enable. Use for the cycle-collection and measurement parts of refcounting-gc-weakrefs.
  • weakref - weak references
    Python Software Foundationreferencereference
    Read weak-reference objects (ref callbacks, proxy objects) and the WeakValueDictionary example. Use for the weak-reference part of refcounting-gc-weakrefs.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels