Idiomatic Python: generators, itertools and the gotchas
Key rules
- Generators are lazy, single-pass and constant-memory.
- Mutable defaults are created once at definition time.
- Closures bind late, so loop variables are read when called.
6 lessons · 4 equations. Each lesson below gives its formulas and key rules; open the lesson for the full explanation.
Key rules
Cross-sectional standardisation
Compute the mean and spread across assets at each timestamp; broadcasting must align on that axis.
Remember
Join row count by key
Duplicate keys multiply rows. Check join cardinality before trusting a backtest or aggregate.
Remember
transform keeps the shape; agg reduces.Key rules
merge_asof with direction="backward" is the "what did I know then" join.Annualised Sharpe after costs
Use net daily returns after spread, fees and turnover costs; the square-root rule assumes weak serial dependence.
Remember
Amdahl’s law
If only fraction of runtime is improved by a factor , the untouched work caps the total speedup.
Remember