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>
Community
  • 1
  • 1
shantanuo
  • 31,689
  • 78
  • 245
  • 403

1 Answers1

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)
NIlesh Sharma
  • 5,445
  • 6
  • 36
  • 53