stdlib

Guides to the Python standard library and its most useful built-in modules.

Python: Context Managers, with Statement & Custom Managers

A Python context manager is the machinery behind the with statement. This post opens it up so you can see exactly how with works under the hood, what the __enter__ and __exit__ methods do, how to build your own context manager with a class or the @contextmanager decorator, and the patterns that stop resource leaks in real apps. “The cleanup code you never run is the bug you never see coming.” Every developer who has leaked a file handle Last ... Read More

Python: Iterators and the Iterator Protocol

Ask Python to loop over a list, a file, or a dictionary, and it plays the same trick every time: it asks the object for a Python iterator, then pulls values one by one until StopIteration says stop. This post unpacks that protocol, __iter__ and __next__ included, and shows you how to build a custom iterator of your own. “Simple is better than complex. Complex is better than complicated.” Tim Peters, The Zen of Python (PEP 20) Last Updated: July ... Read More

Python: Generators with yield, Expressions, Pipelines

Learn the python generator yield pattern. A python generator is a function that produces values lazily with yield. Understand generator expressions, pipelines, send(), and why generators use almost no memory compared to lists. “Make it work, make it right, make it fast. In that order.” Kent Beck, Extreme Programming Last Updated: July 2026 | Tested on: Python 3.14.6 | Difficulty: Intermediate | Reading Time: 16 minutes Think of a Netflix show. You do not download the whole season before you ... Read More