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
- 3.1 Names, identity, mutability, copying and hashability explain · measure4 h
- 3.2 Argument binding, scope and closures explain3 h
- 3.3 Attribute lookup, object protocols and dataclasses explain · implement5 h
- 3.4 Iterators, generators and lazy pipelines explain · implement4 h
- 3.5 Decorators, wrappers and parameterized decorators implement3 h
- 3.6 Context managers, exceptions and deterministic cleanup implement4 h
- 3.7 Type hints, generics, protocols and narrowing explain · implement3 h
- 3.8 Imports, virtual environments and src-layout packaging explain · implement4 h
- 3.9 Reference counts, cycle collection and weak references explain · measure2 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 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 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 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 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 Read the
@dataclassparameters (eq,order,frozen,slots),field()withdefault_factory, frozen instances, post-init processing,replace,asdictandastuple. Use for the validated frozen dataclass in attributes-protocols-dataclasses. - typing - type hints and protocols Read Protocols for structural typing and
runtime_checkablelimits, Generics forTypeVargenerics, andassert_typefor narrowing checks. Use for theProtocoland narrowing parts of type-hints-protocols; PEP 695 syntax itself is in the language reference. - gc - cycle garbage collector Read the cycle collector interface:
collect,get_count,garbageanddisable/enable. Use for the cycle-collection and measurement parts of refcounting-gc-weakrefs. - weakref - weak references Read weak-reference objects (
refcallbacks,proxyobjects) and theWeakValueDictionaryexample. 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
- Scaffold a src-layout package with
uv init, build its wheel withuv build, install the wheel into a fresh environment and run its console entry point outside the repository, showing the installed copy is imported. - 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, withmypy --strictclean. - 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
tracemallocagainst a list-building version. - 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.
- 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.
- Seed a cleanup defect where a stored bound method keeps a cycle alive, diagnose it with a
weakrefprobe and the collector, then fix it so the regression test passes with the collector disabled.
Deliverables
- The
taskrunnerrepository: src-layout package, wheel, and aREADMEwith install and import evidence. - Tests for validation, registration, laziness, cleanup and causes, with
mypy --strictclean. - 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
| Criterion | Pass | Strong |
|---|---|---|
| Installable package | The 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 model | TaskSpec 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 laziness | Cleanup 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 defects | Both 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.