12

Earlier today, I asked a question about the way Python handles certain kinds of loops. One of the answers contained disassembled versions of my examples.

I'd like to know more. How can I disassemble my own Python code?

Community
  • 1
  • 1
A. Dorton
  • 1,185
  • 1
  • 9
  • 7

3 Answers3

13

Look at the dis module:

def myfunc(alist):
    return len(alist)

>>> dis.dis(myfunc)
  2           0 LOAD_GLOBAL              0 (len)
              3 LOAD_FAST                0 (alist)
              6 CALL_FUNCTION            1
              9 RETURN_VALUE
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
3

Besides using dis as module, you can also run it as command line tool

For example, on windows you can run:

c:\Python25\Lib\dis.py test.py

And it will output the disassembed result to console.

kcwu
  • 6,778
  • 4
  • 27
  • 32
2

Use the dis module from the Python standard library (import dis e.g. in an interactive interpreter, then dis.dis any function you care about!-).

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395