[Tutor] Printing the code of a function
spir
denis.spir at free.fr
Mon Dec 29 17:20:56 CET 2008
On Mon, 29 Dec 2008 09:18:43 -0000
"Alan Gauld" <alan.gauld at btinternet.com> wrote:
>
> "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
I would do it so by parsing source file:
* (convert indents to tabs (easier to parse))
* search for a line that start with (n indents) + "def func_name"
* record all following lines that start with (n+1 indents) or more
* stop where a line starts with (n indents) or less
denis
------
la vida e estranya
More information about the Tutor
mailing list