2024-04-26 23:44:28 发布
网友
假设我有一个python文件
def ihavefile(): i = 1 def anotherfile(): pass
我想从文件中提取所有函数,比如ihavefile和anotherfile。我该怎么做?我是逐行读取文件,然后写一个正则表达式,还是有更好的方法?你知道吗
ihavefile
anotherfile
举个例子:
from inspect import getmembers, isfunction, getsource import your_module print getmembers(your_module, isfunction) # [('anotherfile', <function anotherfile at 0x028129B0>), ('ihavefile', <function ihavefile at 0x027F3570>)] for name, func in getmembers(your_module, isfunction): print getsource(func)
举个例子:
相关问题 更多 >
编程相关推荐