Skip to the solution
HardDSA17 min read

How does CPython execute your code?

720 views

The problem

Question presented to candidate: "When I type python app.py, what actually happens between the text in that file and the program running? Is Python compiled or interpreted, what are bytecode and .pyc files, and what changed in CPython 3.11 to make it faster?"

What a strong answer should cover:

  • The pipeline: the tokenizer turns text into tokens, the parser builds an AST, the compiler turns the AST into a code object holding bytecode, and the interpreter's eval loop runs that bytecode
  • Python is "compiled to bytecode, then interpreted": the whole file is compiled first, so a SyntaxError anywhere stops even line 1, while a NameError only appears when that line runs
  • The eval loop is a stack machine: instructions such as LOAD_FAST, BINARY_OP, STORE_FAST push and pop a value stack inside a per-call frame; dis shows them
  • Imported modules cache bytecode in __pycache__/name.cpython-311.pyc (16-byte header, then a marshalled code object); the script you run directly is not cached, and a .pyc only skips compiling, it does not make code run faster
  • Bytecode is a CPython implementation detail that changes between versions (different instructions in 3.11, 3.12 and 3.13 for the same function)
  • 3.11's specializing adaptive interpreter (PEP 659) rewrites hot instructions into type-specific ones such as BINARY_OP_ADD_INT; an experimental JIT followed in 3.13 (PEP 744)

Code / implementation expected: Optional โ€” reading dis output for a small function and explaining each instruction is the usual exercise.

  • cpython
  • internals
  • bytecode

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

17 min read ยท 11 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

PythonFrom source text to bytecode: tokens, AST, code object and dis
PythonA toy eval loop running real bytecode, plus the adaptive interpreter

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 126 of 139 in Python

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Python questions
Up nextHard

How does multiprocessing share data between processes?

Continue