The specialising interpreter after a warm-up
Compare with the generic BINARY_OP in the first example. The adaptive form exists only after the function has run enough times.
CPython compiles your source into bytecode, a list of small instructions for a stack-based virtual machine, and stores it in a code object. The dis module prints that bytecode, which shows what a line really costs and how the specialising interpreter speeds up hot code.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Print the opcode names of lambda x: x + 1 with dis.get_instructions.
Print the co_varnames and co_consts of a function that has a parameter n and a local string text.
Compile the expression "2 * 21" in eval mode, print its constants, and evaluate it.
Explain it without notes
What is bytecode, and who runs it?
What does a stack-based virtual machine do for the expression a * b + 1?
What is the specialising adaptive interpreter, and what changed in Python 3.11?
Why can x = 6 * 7 run no multiplication at run time?
Why does a try block cost almost nothing when no exception is raised, and what does that tell you about EAFP style?
Expected output
4 RESUME_CHECK 0
5 LOAD_FAST_LOAD_FAST 1 (a, b)
BINARY_OP_ADD_INT 0 (+)
RETURN_VALUE