5

我正在尝试使用inspect.getmembers来检查文件中的类和函数。问题是我不知道如何在inspect.getmembers不使用import. 那是因为我每次都需要指定不同的文件名

代码与此类似:

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
4

1 回答 1

6

为了检查一个模块,你必须以某种方式执行它;否则,文件中的定义将不可用。

您可以使用module = __import__(modname)按名称导入模块,或module = imp.load_source("__inspected__", path)按路径导入模块。

于 2012-10-21T02:21:24.643 回答