Python Classes and Dataclasses Cheat Sheet
Define Python classes, manage instance and class state, add properties and alternate constructors, use inheritance, and model records with dataclasses.
Open cheat sheetPython · 9 guides
Define functions, classes, protocols, types, control flow, and reusable behavior.
Focused references
Each guide includes copy-ready syntax, detailed examples, and authoritative references.
Define Python classes, manage instance and class state, add properties and alternate constructors, use inheritance, and model records with dataclasses.
Open cheat sheetManage files, locks, temporary state, cleanup stacks, and custom resources with with, contextmanager, closing, suppress, and ExitStack.
Open cheat sheetBuild composable Python callables with closures, nonlocal state, metadata-preserving decorators, factories, partial, singledispatch, and callable objects.
Open cheat sheetModel closed sets of Python constants with Enum, StrEnum, IntEnum, Flag, aliases, auto-generated values, validation decorators, and robust lookup behavior.
Open cheat sheetRaise, catch, inspect, chain, and clean up after Python exceptions with specific handlers, else, finally, custom errors, and context managers.
Open cheat sheetDefine, call, document, annotate, and compose Python functions with safe defaults, flexible arguments, callbacks, closures, and decorators.
Open cheat sheetIntegrate Python classes with representation, truth testing, equality, hashing, ordering, container access, descriptors, and attribute lookup protocols.
Open cheat sheetRoute structured Python data with match and case using literals, captures, sequences, mappings, class patterns, OR patterns, AS patterns, guards, and enum constants.
Open cheat sheetAnnotate Python functions, containers, unions, callables, generics, protocols, type guards, aliases, and dataclass-like records without confusing hints with runtime enforcement.
Open cheat sheet