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