CPython compiles your `.py` source to bytecode (`.pyc` in `__pycache__/`) on first import. This is not machine code: it is a sequence of opcodes for the CPython stack-based virtual machine. The VM interprets these opcodes one by one. This is why CPython is slower than compiled languages: every operation goes through the interpreter loop. Tools like Cython, Numba and PyPy bypass this by compiling to actual machine code.
import dis
# I inspect the bytecode of a list comprehension
def squares(n):
return [x * x for x in range(n)]
dis.dis(squares)
# Shows: GET_ITER, FOR_ITER, LOAD_FAST, BINARY_OP, LIST_APPEND
# The comprehension creates an inner code object visible in the outputdis.dis() reveals how Python compiles comprehensions, closures and generators
Talk Python To Me: expert interviews on Python internals, tools and the ecosystem