Python programs operate on objects with explicit input, ownership and failure contracts.
Learning roadmap
This section contains 40 written lessons or quiz banks. Programs are verified on CPython 3.14.6. Standard-library lessons target 3.11+ syntax; lessons using data, web, machine-learning or testing packages state their tested dependencies separately. Framework programs use local test clients, not a deployed service. Read the stated limits before adapting a fixture into an application.
Deferred calls and cleanup
Read the contract, run its program and inspect the failure case.
- Python generators: lazy iteration does not make retained output free
- Python decorators: preserve metadata and expose wrapper behavior
- Python closures: capture loop values at the intended time
- Python context managers: clean up on success and failure
- Python dataclasses: frozen fields require an immutable value model
Task and worker ownership
Read the contract, run its program and inspect the failure case.
- Python asyncio TaskGroup: cancel sibling work and retain failure evidence
- Python thread pools: collect results and observe worker failures
Object protocols and lookup
Read the contract, run its program and inspect the failure case.
- Python iterators: exhaustion and repeatable collection ownership
- Python descriptors: field validation and attribute precedence
- Python super and method resolution: cooperate across mixins
- Python Protocol: structural intent without runtime validation
Processes, locks and async budgets
Read the contract, run its program and inspect the failure case.
- Python process pools: importable workers and serialization costs
- Python subprocess: argument vectors, exit codes and bounded fixtures
- Python locks: protect the complete inventory transition
- Python asyncio.Queue: backpressure and completion accounting
- Python asyncio timeout: cancellation and cleanup ownership
Layout, lifetime and resource acquisition
Read the contract, run its program and inspect the failure case.
- Python slots: attribute layout without deep immutability
- Python weak references: a cache does not own its values
- Python ExitStack: unwind partially acquired resources
Class construction, grouped failures and execution context
Read the contract, run its program and inspect the failure case.
- Python metaclasses: validate a class before registering its wire format
- Python ABC: prevent construction of an incomplete implementation
- Python ExceptionGroup: handle selected failures and retain the rest
- Python ContextVar: task-local labels without sharing one global binding
- Python lru_cache: bound retention and include the revision in the key
Declaration hooks, producer lifetime and narrowing ownership
Read the contract, run its program and inspect the failure case.
- Python __init_subclass__: validate declarations without a custom metaclass
- Python async generators: close a suspended producer when leaving early
- Python TypeGuard: a static narrowing claim is not lasting ownership
Suspension, cancellation and shared predicates
Read the contract, run its program and inspect the failure case.
- Python generator send and close: suspension retains state until cleanup
- Python asyncio shield: caller cancellation does not transfer task ownership
- Python Condition: wait for protected state, not for a notification count
Transferred cleanup and type protocols
Read the contract, run its program and inspect the failure case.
- Python ExitStack pop_all: transfer cleanup without discarding it
- Python ExceptionGroup split: retain the nested failure structure
- Python singledispatch: dispatch follows the first argument’s type
- Python rich comparison: return NotImplemented for unsupported operands
Async workers and cached state
Read the contract, run its program and inspect the failure case.
- Python asyncio.to_thread: carry context into blocking work
- Python cached_property: delete the instance value when source state changes
- Python asyncio.gather: a sibling can continue after the first failure
Buffered readers and deadlines
Read the contract, run its program and inspect the failure case.
- Python itertools.tee: a lagging reader retains buffered values
- Python asyncio.timeout: catch the deadline outside the context
Copying records without copying ownership
Read the contract, run its program and inspect the failure case.
Continue learning
Move between tutorial, collections, advanced material and practice using the subject tabs. The sidebar changes with each section; related examples keep one canonical lesson URL.
