Reputation: 27
I'm trying to understand how Python bytecode is executed line by line using the dis module. I've written a simple function, and I'm analyzing the disassembled bytecode for it, but I’m having trouble understanding some parts of the output.
Here is my code:
import dis
def sum(a, b):
c = a + b
print(f"the sum of a and b is {c}")
sum(3, 45)
dis.dis(sum)
The output of dis.dis(sum) is as follows:
2 RESUME 0
What I understand:
The bytecode starts at line 2 and then moves to line 3. Line 3 is a "superinstruction" that loads the values of a and b into the stack, performs addition, and then stores the result in c in array at index.
My confusion:
In the line LOAD_FAST_LOAD_FAST 1 (a, b), I understand that the LOAD_FAST_LOAD_FAST instruction is loading the values of a and b into the stack, but what does the 1 before (a, b) mean? I know that STORE_FAST 2 (c) means it stores the value of c in the local variable array at index 2, but what exactly does the number 1 mean in the LOAD_FAST 1 instruction? What does 0 in BINARY_OP mean? Can someone please help me understand this bytecode step by step? It is super confusing for me.
Upvotes: 0
Views: 27