Skip to main content Brad's PyNotes

Posts

2026

  1. http.client Module and Requests Library

    TL;DR

    The http.client module provides low-level HTTP protocol client functionality, but Python’s documentation recommends using the third-party Requests library instead for higher-level HTTP operations. Requests offers automatic connection pooling, elegant API design, and handles the complexities that make http.client fiddly to use directly.

  2. PEP 525 - Asynchronous Generators

    TL;DR

    PEP 525 introduced asynchronous generators to Python 3.6, enabling functions that combine async def with yield statements. This feature simplifies creating asynchronous data sources by replacing verbose iterator classes with concise generator syntax, delivering approximately 2.3x better performance.

  3. Socket Module

    TL;DR

    The socket module provides low-level access to BSD sockets for network programming. It supports TCP and UDP protocols across IPv4 and IPv6, offering both connection-oriented and connectionless communication patterns for building custom network applications.

2025

  1. Built-in Types: str, list, dict, and set

    TL;DR

    Python’s built-in types (str, list, dict, set) come packed with dozens of methods for manipulation, searching, and transformation. Dictionaries maintain insertion order since Python 3.7, and many lesser-known methods like dict.setdefault(), str.removeprefix(), and set operations on dictionary views can simplify common patterns.

  2. PEP 420 - Implicit Namespace Packages

    TL;DR

    PEP 420 introduced implicit namespace packages, allowing Python packages to be split across multiple directories without requiring an __init__.py file. The import machinery automatically discovers and combines all portions of the package, enabling flexible distribution and avoiding file conflicts.

  3. gc Module - Garbage Collection Control

    TL;DR

    The gc module provides an interface to Python’s garbage collector, allowing you to manually trigger collection, disable automatic collection, debug memory leaks, and tune performance. It’s particularly useful for finding reference cycles, optimizing memory usage in long-running processes, and understanding what objects are consuming memory.

  4. PEP 448: Additional Unpacking Generalizations

    TL;DR

    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.

  5. Built-in Exceptions

    TL;DR

    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!).

  6. Codecs Module: Mastering Text Encoding and Decoding

    TL;DR

    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.

  7. Tutorial: Errors and Exceptions

    TL;DR

    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.

  8. Difflib Module

    TL;DR

    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.

  9. PEP 621: Storing Project Metadata in pyproject.toml

    TL;DR

    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.

  10. PEP 3105: Make print a Function

    TL;DR

    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.

  11. PEP 257: Docstring Conventions for Self-Documenting Code

    TL;DR

    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.

  12. PEP 3134: Exception Chaining and Embedded Tracebacks

    TL;DR

    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.

  13. asyncio vs anyio

    TL;DR

    anyio is an async library that runs on top of asyncio or Trio, providing a unified API with structured concurrency. It solves asyncio’s design limitations while offering backend portability.

  14. Enumerate Function

    TL;DR

    The enumerate() function adds automatic counters to any iterable, returning tuples of (index, value) pairs that make loops cleaner and more Pythonic.

  15. Concurrent Futures

    TL;DR

    The concurrent.futures module provides a simple, high-level interface for executing tasks concurrently using either threads or processes, making parallel programming accessible without dealing with low-level threading or multiprocessing details.

  16. Math Module: Mathematical Functions and Constants

    TL;DR

    The math module provides mathematical functions like sin(), cos(), sqrt(), and constants like pi and e for scientific and mathematical computations.

    Interesting!

    The math.isclose() function solves floating-point comparison issues by checking if two numbers are “close enough” rather than exactly equal, essential for robust numerical code.