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.
- Python Tutorial
- Data Types & Collections
- Interview Questions
- Exercises
- Examples
- Quizzes
- Projects
- Cheatsheet
- DSA in Python
- Packaging & Tools
- Data Analysis
- Web Applications
- Testing & Quality
Concurrency and immutable-state boundaries
- Python asyncio Semaphore: bound active work, not queued tasks
- Python Future cancellation: queued work versus running work
- Python Queue task_done: completion accounting includes shutdown items
- Python frozen dataclass: field rebinding stops, nested mutation remains
Process and data boundaries
- Python subprocess timeout: kill and reap the timed-out child
- Python tar preflight: reject links and path escapes before extraction
Lifetime and I/O boundaries
- Python multiprocessing spawn: keep startup behind the main guard
- Python ContextVar tasks: capture request context at task creation
- Python weak-value cache: an entry does not own its result
- Python socket idle timeout: bound one blocking receive
Python 3.13 and 3.14 contracts
- Python Queue.shutdown: drain accepted work before workers exit
- Python batched strict: reject an incomplete import group
- Python copy.replace: revise one field without cloning owned data
- Python deferred annotations: inspect forward names without resolving them
- Python Executor.map buffersize: pause submission when results wait
- Python heapq max-heap: keep highest-priority scores at the root
Runtime, profiling, and ownership
- Python free-threaded builds: lock the whole shared-state update
- Python InterpreterPoolExecutor: pass results across isolated runtimes
- Python asyncio call graphs: inspect a live task without retaining frames
- Python ctypes.memoryview_at: keep native pointer storage alive
- Python partial Placeholder: leave a positional amount open
Native and platform boundaries
- Python free-threaded diagnostics: separate build capability from current GIL state
- Python signal handlers: install from the main interpreter thread
- Python audit hooks: observe an owned file open without claiming a sandbox
Bounded streams and storage controls
- Python asyncio byte frames: reject an oversized length before reading a body
- Python asyncio delimiter limits: an overrun leaves the bytes buffered
Cross-thread scheduling and cleanup lifetime
- Python AsyncExitStack: unwind acquired resources after a later open fails
- Python asyncio loop callbacks: submit from a worker with call_soon_threadsafe
- Python thread-local state: a reused pool worker retains the previous job's value
- Python eager asyncio tasks: a coroutine can finish before create_task returns
- Python weakref.finalize: run an idempotent cleanup callback once
- Python RLock: permit nested ledger methods without dropping the outer lock
- Python run_coroutine_threadsafe: submit to a live loop and own shutdown
- Python asyncio.to_thread cancellation: the blocking worker can outlive its awaiter
Standard-library state and ownership contracts
- Python ContextVar tokens: restore nested request state exactly
- Python itertools.groupby: contiguous runs are not global groups
- Python bounded deque: append silently evicts the opposite end
- Python dataclass InitVar: validate and derive state without storing input
- Python SQLite row factories: reject duplicate output names before mapping
- Python QueueListener: stop the logging worker after queued records drain
- Python threading.Barrier: abort a failed phase so peers do not wait forever
- Python decimal localcontext: keep a rounding rule inside one calculation
- Python bisect key: search with a key value, insert with a record
- Python lru_cache: callers share the same cached mutable result
