2

Possible Duplicate:
How can I get the source code of a Python function?

How do I return the code of my function? The func_name method works as expected but not the func_code

>>> def testthis(): print 'test successful'
>>> testthis()
test successful

>>> testthis.func_name
'testthis'
>>> testthis.func_code
<code object testthis at 0124D728, file "<pyshell#281>", line 1>
0

1 Answer 1

1

You can use

inspect.getsourcelines(function_name)

for getting he code.

In [1]: def testthis(): print "hello"

In [2]: import inspect

In [3]: inspect.getsourcelines(testthis)
Out[3]: ([u'def testthis(): print "hello"\n'], 1)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.