Key Terms
| Term | In one line |
|---|---|
| CPython | The reference implementation in C; PyPy is JIT, Jython on JVM, IronPython on CLR |
| GIL | One thread executes Python bytecode at a time; released during I/O |
== vs is |
Value equality (__eq__) vs identity (same object); id() gives the address |
| MRO / C3 | The deterministic order attribute lookup walks the inheritance graph |
| Descriptor | An object defining __get__/__set__ — the machinery behind property |
__slots__ |
Replaces the per-instance __dict__ with a fixed layout; big memory win at scale |
| Protocol | Structural subtyping — “has these methods”, checked statically, no inheritance needed |
| Late binding | Closures capture the variable, not its value — the classic loop-in-lambda bug |
| EAFP | “Easier to ask forgiveness” — try/except — the Pythonic default over LBYL checks |
| Generator | Lazy iterator; yield from delegates properly to a sub-generator |
Numbers That Matter
| Operation | Complexity | Trap |
|---|---|---|
lst[i], lst.append, lst.pop() |
O(1) | — |
lst.insert(0, x), lst.pop(0) |
O(n) | Use collections.deque for queue behaviour |
x in lst |
O(n) | vs x in set / x in dict = O(1) — the most common interview catch |
d[key], d[key]=v, del d[key] |
O(1) avg | — |
| Dict load factor | 2/3 | Resize is 4× for small dicts, 2× for large |
| Dict layout | Compact since 3.6, ordered guaranteed 3.7 | Sparse index array + dense entries array |
| Small tuple cache | Free list up to length 20 | Why tuples beat lists for fixed records |
| Thread creation | ~1 ms | vs a process, which is far heavier |
Decision Rules
- The concurrency decision in one line: I/O-bound and many connections →
asyncio. I/O-bound and simple/blocking libraries → threads. CPU-bound →multiprocessing/ProcessPoolExecutor. Threads on CPU-bound work can be slower than serial, because of GIL contention plus switching overhead. - Worked examples: 1,000 URLs to scrape →
asyncio+aiohttp. 100 image thumbnails →ProcessPoolExecutor. - Use a set or dict for membership.
inon a list is a linear scan; converting to a set once is almost always the fix for an accidentally quadratic loop. - Reach for
__slots__when you have hundreds of thousands of small objects — it removes the per-instance dict. Don’t reach for it on a handful of objects; you lose dynamic attributes for nothing. - Reference counting frees most objects immediately; the generational GC exists only to collect reference cycles. That’s the full answer to “how does Python manage memory.”
functools.wrapson every decorator, always — without it you silently destroy__name__,__doc__and the signature, which breaks introspection, docs and some frameworks.- Default to EAFP. LBYL check-then-act is both slower and racy — the thing you checked can change before you act on it.
- Generators for pipelines over large data: constant memory instead of materializing each stage.
Say This Out Loud
“The GIL means only one thread executes Python bytecode at a time, so threading gives you no parallelism on CPU-bound work — it can actually be slower than running serially once you count contention and context switching. But the GIL is released during I/O, so for I/O-bound work threads are genuinely concurrent. That’s why the decision is: CPU-bound goes to processes, I/O-bound with many connections goes to asyncio, and I/O-bound against blocking libraries goes to threads.”
“Reference counting handles almost everything — an object’s count hits zero and it’s freed immediately and deterministically. The generational garbage collector exists for one job: collecting reference cycles that counting alone can never reclaim. When someone asks about a memory leak in Python, cycles and lingering global references are the first two things I look for.”
“The late-binding gotcha catches everyone once. A closure captures the variable, not its value, so building lambdas in a loop gives you N functions that all see the final value. The fix is a default argument to bind at definition time — and knowing why is the difference between memorizing the fix and understanding Python’s scoping.”