从Python fi中提取信息

2024-04-26 23:44:28 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个python文件

def ihavefile(): i = 1
def anotherfile(): pass

我想从文件中提取所有函数,比如ihavefileanotherfile。我该怎么做?我是逐行读取文件,然后写一个正则表达式,还是有更好的方法?你知道吗


Tags: 文件方法函数defpassanotherfileihavefile
1条回答
网友
1楼 · 发布于 2024-04-26 23:44:28

举个例子:

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)

相关问题 更多 >