[Tutor] Printing the code of a function

Michiel Overtoom motoom at xs4all.nl
Mon Dec 29 03:03:17 CET 2008


wormwood_3 wrote:

 > I am wondering if there is a way to
 > print out the code of a defined function.

When Python compiles source code, it doesn't store the source code 
itself; only the compiled intermediate code. With the 'dis' package you 
can disassemble that:

def foo():
     print "Show me the money."

import dis
dis.dis(foo)

 >>>
   2           0 LOAD_CONST               1 ('Show me the money.')
               3 PRINT_ITEM
               4 PRINT_NEWLINE
               5 LOAD_CONST               0 (None)
               8 RETURN_VALUE
 >>>


-- 
"The ability of the OSS process to collect and harness
the collective IQ of thousands of individuals across
the Internet is simply amazing." - Vinod Vallopillil
http://www.catb.org/~esr/halloween/halloween4.html


More information about the Tutor mailing list