Demo entry 6636918

python disassembly

   

Submitted by anonymous on Aug 30, 2017 at 00:56
Language: Python 3. Code size: 764 Bytes.

>>> # https://kate.io/blog/2017/08/24/python-constants-in-bytecode/
>>> import dis
>>> def disassemble(code_str):
...   dis.dis(compile(code_str, '', 'single'))
...
>>> disassemble('1000 is 1000')
  1           0 LOAD_CONST               0 (1000)
              2 LOAD_CONST               0 (1000)
              4 COMPARE_OP               8 (is)
              6 PRINT_EXPR
              8 LOAD_CONST               1 (None)
             10 RETURN_VALUE
>>> disassemble('(10 ** 3) is (10 ** 3)')
  1           0 LOAD_CONST               3 (1000)
              2 LOAD_CONST               4 (1000)
              4 COMPARE_OP               8 (is)
              6 PRINT_EXPR
              8 LOAD_CONST               2 (None)
             10 RETURN_VALUE

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).