Python Learning Guide

Syntax, frameworks, packages — internals explained at code level

📖 Syntax & Basics

🔄

Generators & yield — How Iteration Works Without Eating Memory

The mechanism behind yield "pausing" function execution

Generators don't create values all at once — they yield one at a time. This is why you can process a 10GB file line by line. Internally, the function's stack frame is preserved while execution pauses/resumes.

🎀

Decorator Internals — What @ Actually Does

A function that takes a function and returns a function — that's all

@decorator is syntactic sugar. Identical to func = decorator(func). A pattern wrapping the original function with closures and *args/**kwargs.

🚪

Context Managers — What the with Statement Actually Does

A protocol guaranteeing resource cleanup via __enter__ and __exit__

with calls __enter__() to acquire the resource, and __exit__() to clean up when the block ends (error or not). contextlib.contextmanager enables generator-based implementation.

🏷️

Type Hints — They Do Nothing at Runtime

Why Python type hints are just "hints" and how Pydantic overcomes this

Python's type hints (: int, -> str) aren't checked at runtime. Only static analysis tools like mypy read them. Pydantic overcomes this with __init_subclass__ and model validation for runtime type checking.

✳️

Keyword-only Arguments — What `*,` Means and Why You Use It

That single asterisk in `def f(a, b, *, c)`

Python's `*,` is a marker meaning "everything after this is keyword-only." Pass them positionally and you get a TypeError. Makes call sites self-documenting and signature changes safe. Ruby has no equivalent because Ruby keyword arguments (`name:`) are keyword-only from the start.