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.