Note that there are some explanatory texts on larger screens.

plurals
  1. POHow should I understand the output of dis.dis?
    primarykey
    data
    text
    <p>I would like to understand how to use <a href="http://docs.python.org/library/dis.html" rel="noreferrer">dis (the dissembler of Python bytecode)</a>. Specifically, how should one interpret the output of <a href="http://docs.python.org/library/dis.html#dis.dis" rel="noreferrer"><code>dis.dis</code></a> (or <a href="http://docs.python.org/library/dis.html#dis.disassemble" rel="noreferrer"><code>dis.disassemble</code></a>)?</p> <p>.</p> <p><em>Here is a very specific example (in Python 2.7.3):</em></p> <pre class="lang-py prettyprint-override"><code>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 </code></pre> <p>I see that <a href="http://docs.python.org/library/dis.html#opcode-JUMP_IF_TRUE_OR_POP" rel="noreferrer"><code>JUMP_IF_TRUE_OR_POP</code></a> etc. are bytecode instructions <em>(although interestingly, <code>BUILD_SET</code> does not appear in this list, though I expect it works as <a href="http://docs.python.org/library/dis.html#opcode-BUILD_TUPLE" rel="noreferrer"><code>BUILD_TUPLE</code></a>)</em>. I think the numbers on the right-hand-side are memory allocations, and the numbers on the left are <a href="http://en.wikipedia.org/wiki/GOTO" rel="noreferrer">goto</a> numbers... I notice they <em>almost</em> increment by 3 each time (but not quite).</p> <p>If I wrap <code>dis.dis("heapq.nsmallest(d,3)")</code> inside a function:</p> <pre class="lang-py prettyprint-override"><code>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 &lt;44&gt; # what is &lt;44&gt; ? 10 DELETE_SLICE+1 11 STORE_SLICE+1 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload