Skip to content Skip to sidebar Skip to footer

Python: Literally "printing" A Function

For reasons I won't get into, I need some way to literally print a function. I know when you run print on a function object you get some complicated <010101045 function> outp

Solution 1:

Use inspect.getsource:

defa():
    passimport inspect
print inspect.getsource(a)

Of course, this only works if you have access to the original source.

If you don't have the original source, the most you could get is the byte code (see dis.dis).

EDIT: The Python interactive shell is one example where inspect does not have access to the source.

Post a Comment for "Python: Literally "printing" A Function"