← All lessons
Python
All Python snippets · 350
Every snippet has its own focus page with an explanation and the output it produces.
Variables & Types · 9
- Dynamic TypingPython variables are dynamically typed, allowing them to reference different data types during execution.
- Flexible DataDynamic typing allows functions to handle multiple types, though type checking is often needed for safety.
- Type MutationWhile variables can change types, mixing incompatible types in operations still raises runtime TypeErrors.
- Type HintsType annotations document the expected type but are not enforced at runtime by standard Python.
- Function SignaturesAnnotating function parameters and return values improves readability and enables static analysis tools.
- Ignored HintsPython ignores type hints at runtime, allowing incompatible types to be passed unless checked manually.
- Immutable IntsIntegers are immutable; assigning an integer to a new variable creates a separate reference.
- Mutable ListsLists are mutable; assigning a list to a new variable creates a second reference to the same object.
- Aliasing BugPassing a mutable object to a function passes the reference, so modifications inside the function affect the original.
Collections · 18
- List CreationLists are ordered, mutable collections that allow duplicate elements.
- List ComprehensionList comprehensions provide a concise way to create lists by filtering and transforming sequences.
- Shallow CopySlicing or using list() creates a shallow copy; nested mutable objects are still shared between the lists.
- Tuple CreationTuples are ordered, immutable collections, often used for fixed sequences of items.
- Tuple UnpackingTuples are ideal for returning multiple values from a function, which can be cleanly unpacked by the caller.
- Mutable TuplesTuples store references to objects; if a contained object is mutable, it can be modified even though the tuple is immutable.
- Dict CreationDictionaries store key-value pairs, allowing fast lookups by key.
- Dict GetUsing get() prevents KeyError by returning a default value if the key is missing.
- KeyError AccessDirectly accessing a missing key raises a KeyError, halting execution if uncaught.
- Set CreationSets are unordered collections of unique elements, automatically removing duplicates.
- Set IntersectionSets provide mathematical operations like intersection and union for efficient data comparison.
- Empty SetUsing empty curly braces {} creates an empty dictionary, not a set; use set() for empty sets.
- Default Dictdefaultdict automatically initializes missing keys with the default factory's value.
- Grouping Datadefaultdict(list) is a common pattern for grouping items without checking if keys exist.
- Phantom KeysAccessing a key in a defaultdict creates it, adding phantom keys if you just meant to check it.
- Counting ElementsCounter is a dict subclass for tallying hashable objects and finding frequencies.
- Log AnalysisCounter is extremely useful for aggregating metrics from data streams like server logs.
- Missing ElementsUnlike standard dicts, Counter returns 0 for missing elements rather than raising KeyError.
Control Flow · 12
- Basic IfStandard conditional logic directs execution based on boolean evaluations.
- User RoleElif chains handle multiple exclusive conditions cleanly without deep nesting.
- Assignment vs EqualityUsing a single equals sign inside a condition causes a SyntaxError; equality checks require double equals.
- For LoopFor loops iterate over sequences or iterables, executing a block a specific number of times.
- Enumerate IterationEnumerate provides both the index and the value when iterating, avoiding manual counter variables.
- Modifying IterablesRemoving items from a list while iterating over it causes elements to be skipped due to index shifting.
- Pattern MatchingMatch statements (3.10+) provide structural pattern matching, offering a cleaner alternative to if-elif chains.
- Destructuring MatchMatch cases can destructure sequences and bind variables, allowing complex conditional logic.
- Unbound VariablesA bare variable name in a match case acts as a catch-all wildcard, binding the value rather than comparing it.
- Assignment ExpressionsThe walrus operator (:=) assigns a value to a variable and returns that value in the same expression.
- While ReadingIt is heavily used in loops to assign and check a condition simultaneously, avoiding redundant function calls.
- Walrus ScopeUnlike variables inside comprehensions, walrus operator assignments leak into the enclosing scope.
Functions · 15
- Default ArgsDefault arguments allow callers to omit parameters, falling back to predefined values.
- Kwargs Passing**kwargs collects arbitrary keyword arguments into a dictionary, allowing flexible function signatures.
- Mutable DefaultsDefault arguments are evaluated once at function definition; mutable defaults persist state across calls.
- Basic ClosureClosures capture variables from their enclosing scope, retaining access even after the outer function returns.
- Function FactoryClosures are used to create specialized functions dynamically based on initial configuration.
- Late BindingClosures capture variables by reference, not value; late binding means they all see the final loop value.
- Basic DecoratorDecorators wrap functions to add behavior before or after execution without modifying the original code.
- Timing DecoratorDecorators using *args and **kwargs can wrap functions with any signature, preserving arguments.
- Lost MetadataWrapping a function hides its original metadata; use functools.wraps to preserve name and docstring.
- Yield KeywordGenerators use yield to return values lazily, pausing execution state between calls.
- Lazy File ReaderGenerators are ideal for streaming large datasets, processing one item at a time without loading everything into memory.
- Exhausted GeneratorGenerators are single-use iterables; once exhausted, they yield no more items and must be recreated.
- Functools Partialpartial freezes some arguments, creating a new function with a simpler signature.
- Configuration CallbacksPartial is excellent for creating specialized utility functions from general ones.
- Kwargs OverridePartial can freeze keyword arguments; later calls can still provide the non-frozen ones.
Object-Oriented Programming · 21
- Basic ClassClasses bundle data and behavior; methods access the instance via the self parameter.
- Init & InstanceThe __init__ method initializes instance state when an object is created.
- Class vs InstanceInstance attributes shadow class attributes; accessing the class directly bypasses the instance override.
- Basic InheritanceSubclasses inherit methods and attributes from parent classes, promoting code reuse.
- Super InitUsing super() ensures parent class initializers are called correctly in inheritance chains.
- Multiple Inheritance MROPython uses C3 linearization for Method Resolution Order, defining the lookup path for multiple inheritance.
- String Representation__repr__ defines the official string representation, useful for debugging and logging.
- Operator OverloadingMagic methods allow objects to interact with built-in operators like +, -, and ==.
- Missing BoolIf __bool__ is missing, Python falls back to __len__; 0 evaluates to False, non-zero to True.
- Getter PropertyThe @property decorator turns methods into read-only attributes for cleaner access syntax.
- Validating SetterSetters allow validation logic to be attached to attribute assignments without breaking the public API.
- Recursive SetterUsing the same name for the property and the backing field causes infinite recursion.
- Basic DataclassDataclasses automatically generate __init__, __repr__, and __eq__ based on type hints.
- Frozen DataclassMaking a dataclass frozen creates immutable instances, useful for configuration and hashable types.
- Default FactoryLike function defaults, mutable class attributes require default_factory to avoid sharing state across instances.
- Classmethod DecoratorClassmethods receive the class as the first argument, often used for alternative constructors.
- Alternative ConstructorClassmethods provide flexible ways to instantiate objects from different data formats.
- Static vs ClassStaticmethods don't receive the class or instance; they are just utility functions grouped inside a class namespace.
- ABC BasicsAbstract Base Classes define interfaces that cannot be instantiated until methods are implemented.
- Interface EnforcementABCs ensure subclasses implement critical methods, enforcing architectural contracts.
- Instantiation ErrorAttempting to instantiate a class with unimplemented abstract methods raises a TypeError.
Advanced Typing · 21
- Union TypesUnion types indicate a value can be one of several types, improving function flexibility.
- Optional ReturnsOptional[T] is shorthand for Union[T, None], explicitly marking values that might be missing.
- Pipe OperatorPython 3.10+ allows the | operator for Unions, but using it in older versions raises TypeErrors.
- TypeVarTypeVars allow writing functions that preserve the relationship between input and output types.
- Generic ClassGeneric classes allow creating containers that are typed for specific elements at instantiation.
- Runtime ErasureType hints are erased at runtime; List[int] and List[str] are both just standard list objects.
- Callable TypeCallable annotations specify the signature of functions or methods expected as arguments.
- Protocol ClassProtocols define structural typing (duck typing) for static analysis, verifying methods exist without inheritance.
- Protocol Runtime CheckStandard Protocols cannot be used with isinstance; they require the @runtime_checkable decorator.
- Literal TypeLiteral types restrict a value to specific constants, acting like type-safe enums for primitives.
- TypedDict StructureTypedDict provides type checking for dictionaries with specific key and value structures.
- TypedDict MutationMypy will flag adding unknown keys to a TypedDict, but at runtime it behaves like a normal dict.
- @overload Decorator@overload signatures are for type checkers; the actual implementation handles the logic at runtime.
- Typed GetitemOverloading is crucial for accurate typing of magic methods that behave differently based on input types.
- Missing ImplementationIf you provide @overload stubs but no actual implementation, Python raises NotImplementedError or NameError at runtime.
- ParamSpec BasicsParamSpec allows forwarding arbitrary parameter signatures, crucial for typing decorators correctly.
- Decorator PreservationWithout ParamSpec, a decorated function loses its specific argument types.
- ParamSpec ConcatParamSpec.args cannot be mixed arbitrarily with other parameters; prepend or append strictly.
- User Defined TypeGuardTypeGuard enables custom functions that narrow types in conditional blocks for static checkers.
- Complex NarrowingTypeGuard is essential for validating untyped data (like JSON) into strict typed structures.
- Runtime vs StaticTypeGuard lies to the static checker; if the runtime logic is wrong, it causes runtime crashes.
Error Handling · 6
- Basic TryTry/Except blocks intercept runtime exceptions, allowing graceful recovery.
- Multiple ExceptionsMultiple exception types can be caught in a single block, with the instance bound to the 'as' variable.
- Broad ExceptionCatching Exception hides bugs like NameErrors; it's better to catch specific exceptions.
- Basic Custom ErrorInheriting from Exception creates domain-specific error types for cleaner handling.
- Validation ErrorCustom exceptions can carry extra context like field names, improving debugging.
- Base ExceptionInheriting from BaseException bypasses standard Exception catches, often breaking application recovery.
Context Managers · 3
- File HandlingThe with statement guarantees resources are cleaned up, like closing files, even if errors occur.
- Database TransactionCustom classes implement __enter__ and __exit__ to define their own context blocks for setup/teardown.
- Suppressed ErrorsReturning True from __exit__ suppresses exceptions raised inside the with block, continuing execution.
Modules & Imports · 6
- Basic ImportImporting a module brings its namespace into the current file, accessing members via dot notation.
- From ImportFrom imports pull specific attributes directly into the namespace, avoiding the module prefix.
- Circular ImportsIf two modules import each other at the top level, Python raises ImportError due to incomplete initialization.
- Init File__init__.py executes when a package is imported, used for initialization or exposing APIs.
- Relative ImportsRelative imports allow modules within a package to reference each other without hardcoding package names.
- Main GuardThe __name__ guard prevents code from running when a module is imported, executing only when run directly.
Concurrency · 9
- Basic ThreadThreads allow concurrent execution, useful for I/O bound tasks.
- GIL LimitationThe Global Interpreter Lock (GIL) prevents true parallel CPU execution in CPython threads, causing race conditions if unsynchronized.
- Daemon ThreadsDaemon threads are abruptly terminated when the main program exits, without cleanup.
- Async AwaitAsync/Await enables cooperative multitasking, pausing execution to allow other tasks to run.
- Gather Tasksasyncio.gather runs multiple coroutines concurrently, collecting their results.
- Missing AwaitForgetting to await a coroutine creates a runtime warning and the task never actually executes.
- Basic ProcessMultiprocessing creates separate OS processes, bypassing the GIL for true CPU parallelism.
- Process PoolPools manage worker processes, parallelizing map operations across CPU cores.
- Serialization ErrorArguments and targets passed to Processes must be picklable; lambdas and local functions fail.
Iterators & Generators · 6
- Custom IteratorImplementing __iter__ and __next__ allows objects to be used in for loops.
- Iter Functioniter() gets an iterator from an iterable, and next() manually advances it.
- StopIterationCalling next() on an exhausted iterator raises StopIteration.
- Delegating Yieldyield from delegates iteration to a sub-generator, flattening nested sequences.
- Tree Traversalyield from vastly simplifies recursive generators like tree traversals.
- Generator ReturnA return statement inside a generator acts as a StopIteration signal, halting execution immediately.
Modern Python · 12
- Basic F-StringF-strings provide a concise and fast way to embed expressions inside string literals.
- Debugging F-StringAdding an equals sign inside f-strings (3.8+) prints the variable name and value, useful for debugging.
- F-String QuotesUsing !r inside an f-string applies repr() to the value, showing quotes and escaping characters.
- Path ObjectPathlib provides an object-oriented way to handle filesystem paths, replacing os.path strings.
- Path ReadingPath objects include methods for common file operations like reading and writing text.
- Path DivisionThe / operator is overloaded by Pathlib to join path components cleanly across operating systems.
- Class MatchingMatch statements can destructure objects if __match_args__ is defined or keyword syntax is used.
- Guard ConditionsGuards (if statements inside case) add boolean conditions to pattern matching cases.
- Wildcard CaptureThe underscore _ acts as a catch-all wildcard that matches anything and doesn't bind the value.
- List Comp WalrusThe walrus operator can bind variables inside comprehensions for filtering and transforming in one step.
- Regex MatchUsing walrus with if statements avoids calling the function twice when checking and using the result.
- Walrus SyntaxThe walrus operator must be enclosed in parentheses to avoid syntax errors with precedence.
Advanced Concepts · 12
- Type MetaclassMetaclasses are 'classes of classes', allowing interception and modification of class creation.
- Singleton PatternMetaclasses can enforce architectural patterns like Singletons by controlling instance creation.
- Init vs CallWhen instantiating a class, the metaclass's __call__ is invoked first, which then calls the class's __init__.
- Descriptor ProtocolDescriptors define how attribute access (__get__, __set__) is handled, powering properties and methods.
- Validated AttributeDescriptors allow reusable validation logic for class attributes without boilerplate in __init__.
- Non-Data DescriptorNon-data descriptors (only __get__) are overridden by instance dictionaries; data descriptors (with __set__) take precedence.
- Basic Slots__slots__ restricts attribute creation to a fixed list, reducing memory footprint.
- Memory OptimizationSlotted classes skip the per-instance __dict__, saving memory in large-scale object creation.
- Dynamic Attribute BlockBecause __slots__ removes __dict__, attempting to set an unlisted attribute raises AttributeError.
- Dynamic AttributeMonkey patching adds or modifies methods on classes at runtime.
- Patching BuiltinsTesting frameworks often monkey patch built-ins like time or requests to create deterministic tests.
- Instance PatchingPatching an instance only affects that instance; patching the class affects all instances.
Advanced Decorators · 4
- Class DecoratorClass decorators intercept class creation, allowing dynamic addition of methods or attributes.
- Plugin RegistrationDecorators are often used to register classes in a global registry for plugin architectures.
- Decorator OrderDecorators apply bottom-up; deco_b runs first, then deco_a wraps the result.
- Preserve Metadata@wraps copies the __name__ and __doc__ from the original function to the wrapper.
Itertools & Functools · 6
- Chain Iterableschain merges multiple iterables into a single sequence without copying data.
- Grouping Datagroupby clusters consecutive elements of an iterable sharing the same key.
- Groupby Sortinggroupby only groups consecutive elements; unsorted data yields duplicate groups for the same key.
- LRU Cachelru_cache memoizes function results, turning recursive exponential algorithms into linear ones.
- Cached FetchCaching expensive I/O operations like config fetches prevents redundant network calls.
- Unhashable Argslru_cache requires arguments to be hashable; lists and dicts raise TypeError.
Collections (Advanced) · 6
- Double-Ended Queuedeque provides O(1) append and pop operations from both ends, unlike lists.
- Bounded QueueA maxlen on a deque automatically discards old items when new ones are added.
- List PopUsing list.pop(0) is O(n) because all elements must shift; always use deque.popleft() for queues.
- Named Tuplenamedtuple creates tuple subclasses with named fields, improving readability.
- CSV ParsingNamedtuples are perfect for representing simple tabular data without writing full classes.
- Tuple ImmutabilityDespite having names, namedtuples are still tuples and cannot be modified after creation.
Memory Management · 6
- Reference CountingPython tracks the number of references to an object; memory frees when it hits zero.
- Cyclic ReferencesReference counting fails on cycles; Python's garbage collector periodically detects and cleans them.
- Del Method GCdel only removes the reference, not the object; __del__ runs only when all references are gone.
- Weak ReferenceWeakrefs allow referencing an object without increasing its reference count.
- Weak CacheWeakValueDictionary allows caching objects that will be garbage collected when no strong refs remain.
- Unreferable TypesBuilt-in types like lists and dicts do not support weakrefs directly.
Advanced AsyncIO · 6
- Async GeneratorAsync generators yield values asynchronously, allowing cooperative multitasking in streaming data.
- Stream APIStreaming API responses via async generators prevents blocking the event loop on large payloads.
- Generator CleanupBreaking out of an async for loop triggers the generator's aclose(), running finally blocks.
- Timeout Handlingwait_for cancels the coroutine if it doesn't complete within the specified timeout.
- Task CancellationCalling cancel() on a task injects a CancelledError inside the coroutine.
- Catching CancelSwallowing CancelledError without re-raising prevents the task from actually being cancelled.
Advanced Typing (3.11+) · 6
- Self TypeSelf indicates the method returns an instance of the exact class it belongs to.
- Builder SelfSelf types are essential for fluent interfaces and builder patterns to maintain subclass types.
- Cls Return TypeBefore Self, typing classmethods returning instances required complex TypeVars bound to the base class.
- Never TypeNever indicates a function never returns normally, either crashing or looping forever.
- Exhaustive CheckNever helps static checkers identify unhandled cases in exhaustive conditional branches.
- Assert NeverFunctions typed as Never cannot have a return statement or reach the end of the function.
Context Managers (Advanced) · 6
- Contextlib Decorator@contextmanager turns a generator function into a context manager, avoiding boilerplate classes.
- Timing ContextContext managers are perfect for wrapping blocks of code with setup and teardown metrics.
- CM ExceptionCode after yield only runs if no exception occurs, or if the exception is suppressed.
- Async Contextasynccontextmanager allows creating async context managers using async generators.
- Async DB TransactionAsync context managers handle resource lifecycles in event-loop-bound applications safely.
- Missing AexitAsync context managers must implement both __aenter__ and __aexit__ coroutines.
Concurrency (Locks & Executors) · 6
- Thread LockLocks ensure only one thread executes a block of code at a time, preventing race conditions.
- Safe CounterWrapping shared mutable state in a lock guarantees atomic updates across threads.
- DeadlockIf two threads acquire the same locks in opposite orders, they will block each other forever.
- ThreadPool ExecutorThreadPoolExecutor manages a pool of threads, simplifying concurrent task execution.
- Concurrent Fetchmap distributes an iterable across worker threads, preserving order of results.
- Executor ShutdownIf not using the context manager (with), you must explicitly call shutdown to release resources.
Metaprogramming (Modern) · 4
- Init Subclass__init_subclass__ is a hook called whenever a class is subclassed, replacing many metaclass uses.
- Auto Registration__init_subclass__ accepts kwargs passed in class definitions, perfect for automatic plugin registration.
- Subclass ArgsArguments passed to the class definition are forwarded to __init_subclass__, not __init__.
- Class Getitem__class_getitem__ allows classes to support subscripting (e.g., MyType[int]) for generic typing.
Dataclasses (Advanced) · 9
- Dataclass InheritanceDataclasses support inheritance, merging fields from base classes into derived ones.
- Model ExtensionExtending dataclasses models hierarchical data structures cleanly without boilerplate.
- Default OrderA dataclass cannot have a non-default field after a base class field with a default.
- Post Init Hook__post_init__ is called automatically after __init__, allowing computed fields to be set.
- Validation HookPost-init hooks are perfect for validating dataclass fields immediately upon instantiation.
- Frozen MutateIn a frozen dataclass, standard assignment fails in __post_init__; use object.__setattr__ to bypass.
- Dataclass SlotsPython 3.10+ allows dataclasses to automatically generate __slots__, reducing memory usage.
- Memory EfficientUsing slots=True on dataclasses creates instances without a __dict__, saving memory for large datasets.
- Slots Default FactoryEven with slots, default_factory works correctly because the slot descriptor handles the assignment.
Functional Programming · 6
- Single Dispatchsingledispatch transforms a function into a generic function, dispatching based on the first argument's type.
- Type SerializerSingle dispatch is ideal for serializers or visitors where logic depends on object type.
- Any DispatchBecause bool is a subclass of int in Python, True will match the int dispatch.
- Map Filtermap and filter apply functions lazily to iterables, returning iterators.
- Data PipelineCombining map with sum or list comprehensions allows functional-style data pipelines.
- Lazy EvalBecause map is lazy, the function does not execute until the iterator is consumed.
Advanced Magic Methods · 3
- Enter ExitThese two magic methods define the behavior of the with statement for custom classes.
- File-like ObjectMocking file-like objects with context managers ensures clean resource handling in tests.
- Del vs Exit__exit__ runs deterministically at block end, while __del__ runs at GC time; always use __exit__.
Standard Library (OS/Sys) · 3
Descriptors (Advanced) · 3
- Data DescriptorData descriptors define both __get__ and __set__, intercepting all attribute access and mutation.
- Typed PropertyDescriptors with __set_name__ provide reusable, type-safe properties without boilerplate.
- Instance ShadowNon-data descriptors (only __get__) are shadowed by instance dictionary entries.
Error Handling (Advanced) · 6
- Exception ChainingThe 'from' keyword chains exceptions, preserving the original traceback for debugging.
- Wrap ExceptionsWrapping low-level errors into domain-specific exceptions while chaining improves API cleanliness.
- Suppress ChainUsing 'from None' explicitly suppresses the exception context, hiding the original cause.
- Suppress Contextsuppress silently ignores specified exceptions, avoiding empty try/except blocks.
- Optional DeleteSuppress is ideal for cleanup operations where failure is an acceptable, ignorable outcome.
- Broad SuppressSuppressing generic Exception hides bugs and skips subsequent code in the block silently.
Enums · 3
Slicing · 3
Generators (Advanced) · 3
- Generator Sendsend() passes values back into the generator, yielding them at the yield expression.
- Accumulator GenGenerators using send can maintain state across interactions, acting as lightweight coroutines.
- Initial NextA generator must be started with next() before send() can be used, else TypeError.
Type Hinting (Advanced) · 3
- Protocol PropertyProtocols can define expected properties, ensuring objects expose specific computed attributes.
- Static Duck TypeProtocols verify structural conformance statically, allowing type-safe duck typing.
- Protocol InheritInheriting from a Protocol makes a concrete class, not a new Protocol; use Protocol explicitly.
Functions (Advanced) · 3
Modules & Imports (Advanced) · 3
Memory & Optimization · 3
- Slots InheritanceSubclasses must also define __slots__ to prevent the creation of __dict__ and retain memory benefits.
- Lightweight DataSlots significantly reduce memory overhead for applications creating millions of small objects.
- Slots DictIf a parent class lacks __slots__, the subclass still gets a __dict__, negating the restriction.
Iteration (Advanced) · 3
- Reversed Iterreversed() provides a reverse iterator for sequences that implement __reversed__ or __len__ and __getitem__.
- Reverse ObjectImplementing __reversed__ allows custom iterables to define their own reverse iteration logic.
- Reversed Slicereversed returns an iterator and doesn't allocate a new list, while [::-1] creates a copy.
AsyncIO (Queues) · 2
Multiple Inheritance · 3
- Diamond MROPython resolves the diamond problem via C3 linearization, ensuring a consistent method resolution order.
- Explicit Supersuper() in multiple inheritance follows the MRO, ensuring each parent __init__ is called exactly once.
- Broken SuperCalling parent classes directly by name bypasses MRO, causing the base class A to be initialized multiple times.
Magic Methods (Advanced) · 6
- Contains MagicImplementing __contains__ allows the use of the 'in' operator for custom membership testing logic.
- Custom CollectionOverloading 'in' provides an intuitive API for checking if custom collections contain an item.
- Missing FallbackIf __contains__ is missing, Python falls back to iterating via __getitem__, which is much slower.
- Dict MissingSubclassing dict and implementing __missing__ allows custom default values for missing keys.
- Auto-Vivification__missing__ can automatically create and return nested dictionaries, useful for nested data structures.
- Get vs Missing__missing__ is only called for explicit key access (d['x']); dict.get() bypasses it and returns None.
Concurrency (Async Advanced) · 6
- Async Lockasyncio.Lock ensures exclusive access to shared resources across concurrent tasks.
- Safe CounterWithout an async lock, concurrent modifications to shared state cause race conditions.
- Lock ReentrantUnlike threading.RLock, asyncio.Lock is not reentrant; acquiring it twice in the same task deadlocks.
- Async Eventasyncio.Event allows one task to signal an event, waking up other tasks waiting on it.
- CoordinatorEvents are useful for coordinating multiple tasks to start processing simultaneously after initialization.
- Event ClearForgetting to call clear() leaves the event set, or calling clear() immediately blocks future waits.
Standard Library (Subprocess) · 3
- Subprocess Runsubprocess.run executes shell commands, capturing stdout and exit codes safely.
- Git CheckoutChecking returncode is essential for scripting to ensure external commands succeeded.
- Shell InjectionUsing shell=True with string interpolation exposes the system to shell injection attacks; always use lists.
Standard Library (Argparse) · 3
- Basic Argumentargparse is the standard library for building user-friendly command-line interfaces.
- Flags DefaultsArguments can have default values and specific types, automatically converting inputs.
- Missing RequiredForgetting a required argument triggers a SystemExit exception as argparse prints help and exits.
Standard Library (Logging) · 6
- Basic LoggingbasicConfig sets up the root logger to output messages to the console at a specified level.
- File LoggingConfiguring a filename redirects logs to a file instead of the console, ideal for production.
- Config OverridebasicConfig is a no-op if the root logger already has handlers; it must be called before any logging.
- Named LoggerCreating named loggers allows hierarchical logging and separate configurations per module.
- Module LoggingUsing __name__ as the logger name automatically tags logs with the module path, aiding debugging.
- PropagationLog messages propagate up to the root logger by default, sometimes causing duplicate logs if handlers are attached at multiple levels.
Typing (Modern Python 3.12+) · 6
- Type StatementThe new 'type' keyword (3.12+) creates explicit, distinct type aliases that are easier to read and export.
- Generic AliasThe type statement supports recursive definitions and generics cleanly without TypeAlias.
- Alias LazyEven with explicit aliases, runtime types remain standard Python objects; the alias is purely for static analysis.
- Generic SyntaxPython 3.12 allows defining generics directly in class/function signatures using [T] without TypeVar.
- Generic FunctionThe new syntax simplifies writing generic functions by declaring the type variable inline.
- Bound SyntaxType parameter bounds can now be specified inline using tuple syntax for runtime constraints.
Typing (Final & Literal) · 3
- Final VariablesFinal prevents reassignment of variables, creating true constants for static analysis tools.
- Final MethodsThe @final decorator prevents subclasses from overriding specific methods, protecting critical logic.
- Runtime FinalFinal is ignored at runtime; variables can still be reassigned, but type checkers will flag it.
Internals & Introspection · 6
- Get Membersinspect.getmembers retrieves attributes of an object, filtering by predicates like isfunction.
- Signature Introspectioninspect.signature extracts parameter names and defaults, useful for building dynamic wrappers or documentation.
- Source Codegetsource relies on the original file; dynamically compiled code or lambdas have no source to retrieve.
- Bytecode Disassemblydis compiles Python code to bytecode and prints the VM instructions, revealing execution details.
- Loop OptimizationDisassembling shows that local variables use faster LOAD_FAST instructions compared to global LOAD_NAME.
- Constant FoldingThe Python compiler performs constant folding at compile time, replacing 2 * 3 with 6 in the bytecode.
Standard Library (Collections) · 6
- ChainMapChainMap groups multiple dictionaries together for fast lookups without copying data.
- Config LayeringChainMap is perfect for layered configurations where environment variables override defaults.
- ChainMap MutateMutations in a ChainMap only affect the first dictionary in the chain, not the one where the key was found.
- Ordered DictOrderedDict maintains insertion order, though standard dicts have preserved order since Python 3.7.
- Move To EndOrderedDict provides specialized methods like move_to_end for LRU caches or queue implementations.
- Equality CheckUnlike standard dicts, OrderedDict equality is order-sensitive; different insertion orders mean unequal dicts.
Standard Library (Array) · 3
- Array ModuleThe array module provides space-efficient arrays of primitive C types, like integers ('i') or floats ('d').
- Memory EfficientArrays of primitives consume significantly less memory than lists of Python integer objects.
- Type ConstraintTyped arrays enforce their type code; appending incompatible types raises a TypeError.
Strings & Bytes · 6
- String EncodeStrings are Unicode; encoding converts them to raw bytes for file I/O or network transmission.
- Bytes DecodeDecoding translates raw bytes back into a string using a specific character set.
- Unicode ErrorDecoding bytes with an incompatible charset raises UnicodeDecodeError; errors='ignore' can bypass this.
- Memory Viewmemoryview creates a view over bytes data, allowing slicing without copying the underlying buffer.
- Zero CopyMemoryviews can mutate array or bytearray buffers directly, offering C-like pointer performance.
- Immutable ViewMemoryviews over immutable objects like bytes cannot be modified; use bytearrays for mutable buffers.
Performance & Optimization · 6
- Local OptimizationLocal variable access is optimized via array indexing in the CPython VM, making it faster than globals.
- Loop HoistingBinding built-ins to local variables inside tight loops can yield significant performance gains.
- Unbound LocalAssigning to a variable inside a function makes it local to the entire function, causing errors if accessed before assignment.
- Join StringsUsing join() is O(n) efficient, whereas repeated += concatenation in a loop is O(n^2) due to immutability.
- List BuilderAccumulating string fragments in a list and joining at the end is the standard fast pattern in Python.
- Loop ConcatStrings are immutable; += in a loop repeatedly allocates new memory, degrading performance exponentially.
Decorators (Advanced) · 6
- Parameterized DecoratorDecorators with arguments require an extra wrapper layer to pass the arguments before the function decorator.
- Retry LogicParameterized decorators are used to configure behavior like retry counts or logging levels dynamically.
- Nesting LevelsA decorator factory (@deco()) requires three levels of nesting, whereas a normal decorator (@deco) requires two.
- Async WrapperDecorating coroutines requires an async wrapper that awaits the original function.
- Async TimerAsync decorators intercept coroutines to add cross-cutting concerns like timing without blocking the event loop.
- Sync On AsyncUsing a synchronous wrapper on an async function returns a coroutine object instead of awaiting it, causing bugs.
OOP (Advanced) · 9
- Del Method__del__ is called when the garbage collector destroys an object, but its timing is not guaranteed.
- Resource CleanupWhile __del__ can close resources, context managers (__exit__) are preferred for deterministic cleanup.
- Del ExceptionsExceptions raised inside __del__ are printed to stderr but ignored, not crashing the program.
- Eq Hash ContractIf two objects are equal via __eq__, they must have the same __hash__ to work in sets and dicts.
- Dict Key ObjectCustom objects can be used as dictionary keys safely only if they implement both __eq__ and __hash__.
- Unhashable ObjectDefining __eq__ without __hash__ sets __hash__ to None, making the object unhashable.
- Getattr Fallback__getattr__ is only called when an attribute is not found via normal lookup, useful for dynamic delegation.
- Lazy Property__getattr__ can compute and cache attributes on the fly, avoiding expensive initialization in __init__.
- Getattribute Recursion__getattribute__ intercepts ALL access; accessing self.name inside it causes infinite recursion unless object.__getattribute__ is used.
Typing (Literal & Strings) · 3
- Literal StringLiteralString (3.11+) accepts only string literals, preventing SQL injection via dynamic strings.
- Safe SQLPassing variables to LiteralString functions fails type checking, enforcing secure query construction.
- Concat LiteralsConcatenating two string literals results in a LiteralString, but concatenating with a variable does not.
Typing (Special Forms) · 3
- TypeAlias KeywordTypeAlias explicitly marks an assignment as a type alias, improving readability over implicit aliases.
- Complex AliasExplicit aliases are excellent for documenting complex union types throughout a codebase.
- Implicit AmbiguityBefore TypeAlias, assignments and class definitions could conflict in static analysis tools.
File I/O (Advanced) · 9
- Seek Positionseek() moves the file pointer to a specific byte or character offset for reading or writing.
- Append DataSeeking to offset 0 with 'whence=2' positions the cursor at the end of the stream for appending.
- Tell Positiontell() returns the current cursor position, which updates after every read or write operation.
- Pathlib GlobPath.glob('*.py') matches files by shell-style patterns, returning Path objects instead of strings.
- Recursive Globrglob searches directories recursively, equivalent to glob('**/*.txt').
- Glob Orderglob() returns paths in arbitrary OS order; wrap in sorted() if you require deterministic ordering.
- Temp Filetempfile creates secure, temporary files that are automatically deleted when closed.
- Persist TempSetting delete=False keeps the temporary file after closing, useful for generating intermediate artifacts.
- Temp DirectoryTemporaryDirectory creates a temporary folder that is automatically removed when the context exits.