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.
⚡ FastAPI
FastAPI Architecture — The Starlette + Pydantic + Uvicorn Stack
The reality behind FastAPI's "fast" claim
FastAPI itself is a "framework on top of frameworks." Starlette handles HTTP, Pydantic handles validation, Uvicorn is the ASGI server. FastAPI ties them together with a type-hint-based interface.
FastAPI Depends() — Dependency Injection via Function Signatures
Injecting DB sessions, auth, pagination as function parameters
FastAPI's Depends() auto-calls/injects functions as default argument values. Supports nesting and guarantees cleanup with generators.
FastAPI async/await — The Difference Between def and async def
Does async def make it faster? It depends on the case
In FastAPI, def runs in threadpool, async def runs on event loop. Async wins for I/O-heavy, def is better for CPU-bound. Supporting both is FastAPI's strength.
🎸 Django
Django ORM Internals — Why QuerySets Are Lazy
Why chaining filter().exclude().order_by() doesn't execute SQL
Django QuerySets don't execute SQL until evaluated. filter/exclude/order_by just stack conditions internally. DB query only fires on list()/for/[0] access.
Django Middleware — The Onion Structure Wrapping Requests/Responses
Why MIDDLEWARE order matters and how chaining works internally
Django middleware wraps like onion layers. Requests go outside-in, responses go inside-out. MIDDLEWARE list order determines execution order.
Django Signals — Event-Based Loose Coupling
How post_save and pre_delete work internally
Django Signals implement the Observer pattern. Auto-call registered receiver functions when models are saved/deleted. Convenient but overuse makes debugging hard.
📦 Popular Packages
requests Internals — What requests.get() Actually Does
The call chain from Session to PreparedRequest to HTTPAdapter to urllib3
requests.get(url) internally creates Session → assembles PreparedRequest → selects HTTPAdapter → actual TCP via urllib3.PoolManager. Understanding these layers lets you properly configure connection pools, timeouts, retries.
Pydantic Internals — How Type Hints Become Runtime Validation
__annotations__ → Rust core (pydantic-core) → validation execution
Pydantic v2 reads __annotations__ when defining model classes to generate validation schemas in Rust-written pydantic-core. Validation runs through this schema when values are set in __init__.
Click — The Cleanest Way to Build Python CLI Tools
How a decorator-based CLI framework works internally
Click defines CLI args/options via decorators. More intuitive than argparse, created by Flask developer Armin Ronacher. @click.command + @click.option completes a CLI.