我正在尝试使用inspect.getmembers来检查文件中的类和函数。问题是我不知道如何在不使用import的情况下将文件名传递给inspect.getmembers。这是因为我每次都需要指定一个不同的文件名
代码如下所示:
def extractName(self,fileName):
for name, obj in inspect.getmembers(FileName):
if inspect.isclass(obj):
print "this is class",name
if inspect.isfunction(obj):
print "this is method",name发布于 2012-10-21 10:21:25
为了检查模块,您必须以某种方式执行它;否则,文件中的定义将不可用。
您可以使用module = __import__(modname)按名称导入模块,或使用module = imp.load_source("__inspected__", path)按路径导入模块。
https://stackoverflow.com/questions/12994366
复制相似问题