Bytecode Manipulation — ELI5

When you write a letter, you use full sentences. But when you send a telegram (the old-fashioned kind), you shorten everything: “ARRIVING TUESDAY STOP BRING CAR STOP.” Same message, but compressed into tiny commands.

Python does something similar with your code. Before running it, Python translates your nice, readable program into a stream of tiny commands called bytecode. Each command is super simple — things like “load this value,” “add these two numbers,” “store the result here.”

You never see this happening. When you run python my_script.py, Python secretly compiles your code into bytecode, then feeds those tiny commands to a machine called the interpreter that executes them one by one. It is like having a translator sit between you and the computer.

Those .pyc files you sometimes see in __pycache__ folders? Those are the saved bytecode versions of your Python files. Python keeps them around so it does not have to re-translate your code every time you run it.

Here is the cool part: you can look at the bytecode. Python has a built-in tool called dis (short for “disassemble”) that shows you the tiny commands:

import dis
dis.dis(lambda x, y: x + y)

This shows you the exact steps Python takes: load x, load y, add them together, return the result.

And if you are feeling adventurous, you can actually change the bytecode — swap instructions, add new ones, or remove existing ones. This is like editing the telegram after it is written but before it is sent. It is tricky and can break things easily, but it gives you incredible power to modify how Python programs behave.

One thing to remember: Bytecode is Python’s secret internal language — tiny instructions that the interpreter actually runs — and Python lets you peek at and even modify these instructions.

pythoncompiler-internalsbytecode

See Also