7 routes42 sections324 units

Section 3 of 7 · Programming & Data Foundations

Python Execution Model

Later ML code is still Python code. Precise reasoning about objects, imports, cleanup and package boundaries prevents silent state errors that framework knowledge cannot repair.

9 units 19 topics 0/9

Units

9 in this section

  1. 3.1 Names, identity, mutability, copying and hashability
    explain · measure
    4 h
  2. 3.2 Argument binding, scope and closures
    explain
    3 h
  3. 3.3 Attribute lookup, object protocols and dataclasses
    explain · implement
    5 h
  4. 3.4 Iterators, generators and lazy pipelines
    explain · implement
    4 h
  5. 3.5 Decorators, wrappers and parameterized decorators
    implement
    3 h
  6. 3.6 Context managers, exceptions and deterministic cleanup
    implement
    4 h
  7. 3.7 Type hints, generics, protocols and narrowing
    explain · implement
    3 h
  8. 3.8 Imports, virtual environments and src-layout packaging
    explain · implement
    4 h
  9. 3.9 Reference counts, cycle collection and weak references
    explain · measure
    2 h

Before moving on

Checkpoint

Build and install a typed src-layout package from a blank directory, then trace and repair one seeded aliasing, cleanup or import defect without trial-and-error edits.

Deliberately out of scope

Leave for later

  • Metaclass frameworks and custom import hooks
  • CPython interpreter implementation details beyond a diagnosed need
  • Supporting multiple packaging backends

Resources for this section

Shared by every unit above

  • Stas' Python Cookbook - selected path
    Stas Bekmancourse
    primary
    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 Foundationreference
    required
    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.
  • Packaging Python Projects
    Python Packaging Authorityimplementation
    required
    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
    AlgoMasterpractice
    support
    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 Foundationreference
    reference
    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 Foundationreference
    reference
    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 Foundationreference
    reference
    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 Foundationreference
    reference
    Read weak-reference objects (ref callbacks, proxy objects) and the WeakValueDictionary example. Use for the weak-reference part of refcounting-gc-weakrefs.

Apply the section

Mini-project · Installable taskrunner package with a typed registry and two seeded defects (8 to 12 h)

Build the installable task runner package the later projects extend, with a typed task registry, a lazy run log and deterministic cleanup. Two seeded defects, one aliasing and one lifetime, train evidence-first diagnosis with a regression test for each.

Steps

  1. Scaffold a src-layout package with uv init, build its wheel with uv build, install the wheel into a fresh environment and run its console entry point outside the repository, showing the installed copy is imported.
  2. Model tasks as a frozen slotted dataclass validated in __post_init__, registered by a decorator that rejects duplicates, and checked against the AlgoMaster Python Data Classes and Decorators exercises, with mypy --strict clean.
  3. Stream the JSON Lines run log through a generator and build the summary from generator expressions, then predict which peak grows with file size and compare peaks with tracemalloc against a list-building version.
  4. Give each run a directory through a context manager that writes a marker and closes the log on every path, map failures to a small chained exception hierarchy, and test every path.
  5. Seed an aliasing defect where a shallow copy shares nested run parameters across runs, write the regression test first, watch it fail, then fix it and explain why the chosen copy strategy removes the sharing.
  6. Seed a cleanup defect where a stored bound method keeps a cycle alive, diagnose it with a weakref probe and the collector, then fix it so the regression test passes with the collector disabled.

Deliverables

  • The taskrunner repository: src-layout package, wheel, and a README with install and import evidence.
  • Tests for validation, registration, laziness, cleanup and causes, with mypy --strict clean.
  • Two defect write-ups with failing and passing regression output, diagnosis evidence and the laziness peak comparison.

Checks

  • rubric · Self-grade with the rubric below

Rubric

CriterionPassStrong
Installable packageThe wheel installs into a fresh environment and the entry point runs outside the repository.A test asserts the imported package is the installed copy, and the README rebuilds it from a clean checkout.
Typed modelTaskSpec rejects bad values and round-trips, duplicates are rejected, and mypy --strict is clean.Decorated tasks keep their own type, and the write-up shows the rejected Protocol typing and why.
Resources and lazinessCleanup and markers hold on every path with causes chained, and the learner's tests cover each path.The peak comparison shows the generator holding flat while the list version grows, as predicted.
Seeded defectsBoth regression tests fail on the seeded code and pass after the fix.Each write-up names the shared reference or cycle behind the bug and why the fix removes it.

Feeds into: The installable typed package, its entry point and the run log that the service and pipeline write; the lifetime write-up seeds the evidence-first debugging report.

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