Part 1 · Section 11
Extras
This section collects small but useful techniques that don’t fit neatly anywhere else. Each one is something you will reach for in real programs: making randomness repeatable so bugs can be reproduced, measuring how fast a snippet runs, reading arguments from the command line, and writing functions whose parameters say exactly what they mean.
In this section you’ll learn:
- why “random” numbers are really pseudo-random, and how a seed makes them repeatable
- picking random elements with
choice,choices(with replacement and weights) andsample(without replacement) - timing small pieces of code with
timeit, and how its separate namespace works (setupandglobals) - choosing meaningful names instead of a blanket
*argsand**kwargs - reading command line arguments with
sys.argv, and building a real interface withargparse - using a private sentinel object to tell “argument not given” apart from “argument was
None” - simulating a
switchstatement withif/elif, dictionaries and a decorator-based registry, plus the modernmatchstatement (3.10+)