The `X | None` type union
`X | None` describes a value of type `X` or the value `None`. In `-> str | None`, it describes a function's return value, not a default.
CKTIS / Python
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
`X | None` describes a value of type `X` or the value `None`. In `-> str | None`, it describes a function's return value, not a default.
Use `X | None` to allow a value of type `X` or `None`. If a class is defined later, quote the entire annotation or use the `__future__` import.
Use `setUp()` to give each test fresh data, then check values with `assertEqual()` and an expected exception with `assertRaises()`.
Check multiple inputs in one test method, label failing cases, and distinguish an equality assertion from a callable-form exception assertion.
Run a test suite or a single test case and use the returned `TestResult` to build a custom summary without parsing the runner’s text report.
In a function definition, `*args` collects extra positional arguments into a tuple. In a call, `*` unpacks an iterable into positional arguments.
In Python 3.12, `*args` collects extra positional arguments into a tuple and `**kwargs` collects unmatched keyword arguments into a dictionary. At the call site, the same symbols unpack arguments.
In a function definition, `*items` collects leftover positional arguments into a tuple. At a call site, `*values` does the reverse: it passes the elements of an iterable as separate positional arguments.
Wrap an existing logger in `logging.LoggerAdapter` with a `team` field so that `info`, `warning`, and `error` calls all carry the same team context.
Use `write()` to build text in memory one piece at a time, `read()` to consume text from the current position, and `getvalue()` to retrieve the whole buffer without rewinding.
AssertionError is a built-in exception raised when an assert statement's condition is false. Assertions check internal program assumptions, not mandatory input validation: optimization can remove them.
Exception is Python’s built-in base class for ordinary exceptions. It lets you define custom error types and catch them together, but does not cover every descendant of BaseException.
IndexError is a built-in exception raised when an attempt to access a sequence element uses an out-of-range index. Learn valid index bounds, the difference between indexing and slicing, and how to catch this exception.
KeyError means that a requested key was not found in a mapping, such as a dict. Learn the difference between square-bracket lookup and get(), and handle missing keys without hiding unrelated errors.
NameError means Python could not find a referenced name in the available scopes. Check spelling, assignment order, and imports. Distinguish NameError from AttributeError. UnboundLocalError is a subclass of NameError.
StopIteration signals iterator exhaustion during next(). Its value attribute can carry a generator's return value. Ending a generator with return is different from allowing an explicitly raised StopIteration to escape its code.
TimeoutError signals that an operation exceeded its time limit. In Python 3.14, asyncio.TimeoutError is a deprecated alias of the same class, not a separate exception. Catch TimeoutError and distinguish a timeout from ordinary task cancellation.
TypeError means that an operation or function received an object of an inappropriate type. Learn how it differs from ValueError and how to handle it without hiding the underlying problem.
UnboundLocalError occurs when a function reads a local variable that has no value bound to it at that point. It is a subclass of NameError; a common cause is an assignment that shadows a variable from an outer scope.
ValueError indicates that an argument has the correct type but an unacceptable value. Learn how it differs from TypeError and how to raise and catch it.
ZeroDivisionError is a built-in exception raised when division or a remainder operation uses a zero divisor. Handle it with except ZeroDivisionError when the program can respond meaningfully to that case.
Computes an absolute value or the magnitude of a complex number.
Obtains the asynchronous iterator used to consume an asynchronous iterable.
Tests successive elements for truth and stops at the first falsy item.