Python 反射 - 我可以使用它来获取方法定义的源代码吗 [英] Python reflection - Can I use this to get the source code of a method definition
本文介绍了Python 反射 - 我可以使用它来获取方法定义的源代码吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
重复...
我有一个成功运行的方法定义,但想在运行时修改它.
I have a method definition which is successfully running, but would like to modify it in runtime.
例如:如果我有方法
def sayHello():
print "Hello"
type(sayHello) 给了我类型函数"的答案.我能得到这个函数对象的源代码字符串吗?是否被视为安全问题?
type(sayHello) gives me the answer 'type function'. Will I able to get the source code string of this function object. Is it considered a security issue ?
推荐答案
使用检查模块:
import inspect
import mymodule
print inspect.getsource(mymodule.sayHello)
该函数必须在您导入的模块中定义.
The function must be defined in a module that you import.
这篇关于Python 反射 - 我可以使用它来获取方法定义的源代码吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文