1

重複の可能性:
Python 関数のソース コードを取得するにはどうすればよいですか?

関数のコードを返すにはどうすればよいですか? func_name メソッドは期待どおりに機能しますが、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>
4

1 に答える 1

1

使用できます

inspect.getsourcelines(関数名)

彼のコードを取得するため。

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

In [2]: import inspect

In [3]: inspect.getsourcelines(testthis)
Out[3]: ([u'def testthis(): print "hello"\n'], 1)
于 2012-08-10T13:08:42.637 に答える