Quick Reference
| Topic | File |
|---|---|
| Dynamic typing, type hints, duck typing | types.md |
| List/dict/set gotchas, comprehensions | collections.md |
| Args/kwargs, closures, decorators, generators | functions.md |
| Inheritance, descriptors, metaclasses | classes.md |
| GIL, threading, asyncio, multiprocessing | concurrency.md |
| Circular imports, packages, init.py | imports.md |
| Pytest, mocking, fixtures | testing.md |
Critical Rules
def f(items=[])shares list across all calls — useitems=Nonethenitems = items or []ischecks identity,==checks equality —"a" * 100 is "a" * 100may be False- Modifying list while iterating skips elements — iterate over copy:
for x in list(items): - GIL prevents true parallel Python threads — use multiprocessing for CPU-bound
- Bare
except:catchesSystemExitandKeyboardInterrupt— useexcept Exception: UnboundLocalErrorwhen assigning to outer scope variable — usenonlocalorglobalopen()without context manager leaks handles — always usewith open():- Circular imports fail silently or partially — import inside function to break cycle
0.1 + 0.2 != 0.3— floating point, usedecimal.Decimalfor money- Generator exhausted after one iteration — can't reuse, recreate or use
itertools.tee - Class attributes with mutables shared across instances — define in
__init__instead __init__is not constructor —__new__creates instance,__init__initializes- Default encoding is platform-dependent — always specify
encoding='utf-8'