使用Ipython获取/编辑函数代码

时间:2014-01-07 00:55:20

标签: ipython

当你输入函数??时,除了信息,你得到函数的源代码,如果有的话。 有没有办法?:

1)将代码作为str对象获取 2)编辑类似于%load

1 个答案:

答案 0 :(得分:0)

假设你有模块和函数名称为“module”和“function”字符串变量,那么

from inspect import getmembers, isfunction, getsource

internal_module = __import__(module)
internal_functions = dict(getmembers(internal_module, isfunction))
source_code = getsource(internal_functions[function])

将为您提供包含模块中所有函数的字典(internal_functions)和包含该特定函数源的字符串(source_code)。

我认为你必须操纵字符串才能编辑这个函数。

相关问题