I would like to understand how to use dis (the dissembler of Python bytecode). Specifically, how should one interpret the output of dis.dis
(or dis.disassemble
)?
.
Here is a very specific example (in Python 2.7.3):
dis.dis("heapq.nsmallest(d,3)")
0 BUILD_SET 24933
3 JUMP_IF_TRUE_OR_POP 11889
6 JUMP_FORWARD 28019 (to 28028)
9 STORE_GLOBAL 27756 (27756)
12 LOAD_NAME 29811 (29811)
15 STORE_SLICE+0
16 LOAD_CONST 13100 (13100)
19 STORE_SLICE+1
I see that JUMP_IF_TRUE_OR_POP
etc. are bytecode instructions (although interestingly, BUILD_SET
does not appear in this list, though I expect it works as BUILD_TUPLE
). I think the numbers on the right-hand-side are memory allocations, and the numbers on the left are goto numbers... I notice they almost increment by 3 each time (but not quite).
If I wrap dis.dis("heapq.nsmallest(d,3)")
inside a function:
def f_heapq_nsmallest(d,n):
return heapq.nsmallest(d,n)
dis.dis("f_heapq(d,3)")
0 BUILD_TUPLE 26719
3 LOAD_NAME 28769 (28769)
6 JUMP_ABSOLUTE 25640
9 <44> # what is <44> ?
10 DELETE_SLICE+1
11 STORE_SLICE+1