Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何查找模块中的子模块调用_Python_Python 2.7_Python 3.x_Module - Fatal编程技术网

Python 如何查找模块中的子模块调用

Python 如何查找模块中的子模块调用,python,python-2.7,python-3.x,module,Python,Python 2.7,Python 3.x,Module,我想在列表中列出所有函数调用,包括子模块调用。在我的例子中,它是os模块的os_列表。我想将os.path模块调用的调用与此一起存储。对于函数调用的标识,我使用“\u call”,用于标识模块的内容 for name in dir(os): attr = getattr(os, name) if hasattr(attr, '__call__'): os_list.append(name) 您可以使用检查对象类型。 对于模块,classinfo参数应为: 当我们

我想在列表中列出所有函数调用,包括子模块调用。在我的例子中,它是os模块的os_列表。我想将os.path模块调用的调用与此一起存储。对于函数调用的标识,我使用“\u call”,用于标识模块的内容

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

您可以使用检查对象类型。
对于模块,
classinfo
参数应为:

当我们讨论这个问题时,你也可以对函数做同样的处理。因此,您的代码如下所示:

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:还包括内置函数。

更清楚一点。您试图查找当前模块或目标模块中使用的所有
os
函数?这是否回答了您的问题?
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)