Welcome!
Welcome to Brad’s Python Notes (PyNotes!).
This is Brad’s Python Notes - a blog covering interesting aspects about Python, covering the Python Tutorial, the Python Standard Libraries and the Python Enhancement Proposals (PEPs).
Welcome to Brad’s Python Notes (PyNotes!).
This is Brad’s Python Notes - a blog covering interesting aspects about Python, covering the Python Tutorial, the Python Standard Libraries and the Python Enhancement Proposals (PEPs).
Context managers handle setup and cleanup automatically using the with statement, implementing __enter__ and __exit__ methods to guarantee resource cleanup even when exceptions occur.
PEP 448 extended Python’s unpacking operators (* and **) in Python 3.5, allowing multiple unpackings in function calls and enabling unpacking directly within list, tuple, set, and dictionary literals. This eliminates verbose workarounds and makes code more concise and readable.
Python’s built-in exceptions form a strict inheritance hierarchy rooted in BaseException, with all user code exceptions inheriting from Exception. Choose specific exception types like ValueError (wrong value) or TypeError (wrong type) rather than generic exceptions, and consider carefully before catching BaseException in user code as it will do things like capture keyboard interrupts (no Ctrl-C for you!).
The codecs module provides functions to encode and decode data between bytes and text using various character encodings (UTF-8, ASCII, etc.), with flexible error handling strategies for dealing with malformed data.
Python separates syntax errors (caught before running) from exceptions (caught during execution). The try/except/finally system lets you catch errors, recover gracefully, and guarantee cleanup code runs.
The difflib module provides tools for comparing sequences (especially text strings) and generating difference reports in various formats. It can find the similarity between strings, produce unified
or context diffs
like Unix diff tools, and identify close matches from a list of possibilities.
PEP 621 established a standardized [project] table in pyproject.toml for declaring Python project metadata. It provided a tool-agnostic, static format that replaced executable setup.py code and tool-specific setup.cfg files with declarative TOML that could be parsed without running code.
PEP 3105 converted Python’s print statement into a built-in function in Python 3.0. This change provided consistency with the rest of Python’s syntax and allowed features like custom separators, file output redirection, and easy replacement with logging systems.
PEP 257 defines standardized conventions for Python docstrings, covering how to document modules, functions, classes, and methods using string literals that become the __doc__ attribute. The conventions emphasize triple double quotes, command-style phrasing, and consistent formatting.
PEP 3134 introduced exception chaining in Python 3, allowing exceptions to preserve their context when new exceptions occur during error handling. The raise ... from syntax creates explicit chains, while Python automatically captures implicit chains, both appearing in tracebacks to simplify debugging.