2017-07-14 77 views
0

我想列出列表中包含子模块调用的所有函数调用。在我的情况下,它是os模块的os_list。我想存储os.path模块调用的调用以及this.For函数调用的识别我正在使用“__call”,什么是用于识别模块。如何找到模块中的子模块调用

for name in dir(os): 
    attr = getattr(os, name) 
    if hasattr(attr, '__call__'): 
     os_list.append(name) 
+0

让它更清晰一点。你正在试图找到所有在当前或目标模块中使用的'os'函数? – CommonSense

回答

1

您可以使用[Python]: isinstance(object, classinfo)检查对象类型。
对于模块的classinfo参数应该是[Python]: types.ModuleType

isinstance(attr, types.ModuleType) 

虽然我们关于这个问题,你可以对功能做同样的。所以,你的代码看起来像:

from types import BuiltinFunctionType, FunctionType, ModuleType 

# ... 

os_list = list() 
for name in dir(os): 
    attr = getattr(os, name) 
    if isinstance(attr, (BuiltinFunctionType, FunctionType, ModuleType)): 
     os_list.append(name) 

@ EDIT0:包括内置的功能也是如此。

+0

这是否回答你的问题? – CristiFati