-
Allison Kaptur
-
"just read the code" is bad advice
-
understanding a codebase is a specialized skill
-
you can practice and improve it
-
analogy to "How to Read a Paper"
-
how do you not read the innards of Python?
-
observation and experimentation
-
play the role of a 19th-century naturalist, coming back from an island to give a talk at the local scientific society
-
take into account history and evolution of the code
-
remember that not everything is intentional
-
observational astronomy: why are we seeing what we're seeing?
-
is it because of what we're looking at, or where we're looking?
-
texts are not (normally) designed to deceive or mislead you
-
but code can and will due to external constraints (deadlines, perf) or mistakes
-
inspect
is a useful module for observation -
inspect.getsource(foo)
equivalent to IPythonfoo??
-
doesn't work on C functions
-
cinspect extends inspect to handle C code
-
use history and changelogs
-
Python used to have a good rep for very clean and readable C code
-
15 years later, perf constrains have changed this
-
but you can go back and look at earlier versions!
-
hg blame -r revnum
for Mercurial changelogs -
look at the source
-
look at the AST
-
look at the bytecode
-
False is False is False
not equivalent to(False is False) is False)
-
original version is actually a ternary compare
-
parenthesized version is not
-
difference is most obvious at bytecode level
-
run experiments and test hypotheses
-
timeit
module runs code snippets many times, best of 3, to minimize measurement errors and startup costs -
python -m timeit -s "foo()"
-
write tests to demonstrate invariants
-
break CPython as much as you want (provided you don't contribute the breakage back)
-
poke stuff and see what happens