[Tutor] Printing the code of a function

Alan Gauld alan.gauld at btinternet.com
Mon Dec 29 10:18:43 CET 2008


"wormwood_3" <wormwood_3 at yahoo.com> wrote

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

Its not reliable but I think you can use

func.func_code.filename
func.func_code.firstlineno

To find the first line of code in the original source file.
Its up to you to figure out the last line though! I guess checking
for the next line with equivalent indentation might work.

But I'm not sure how much good it would do you... unless you
were writing a debugger maybe?

Alan G




More information about the Tutor mailing list