Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.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_File_Python 2.7_Operating System - Fatal编程技术网

在python中从模块获取函数名

在python中从模块获取函数名,python,file,python-2.7,operating-system,Python,File,Python 2.7,Operating System,用于执行--来自audit.status导入状态 其中,audit.status中的“status”是模块名,最后一个“status”是类名 我使用的是\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu导入作为 temp = __import__ ("audit.status"), globals=(), locals=(), fromlist=["status"] classname = temp.status 它工作得很好,但是如何使用在下面语句中使用上述方法获

用于执行--来自audit.status导入状态 其中,audit.status中的“status”是模块名,最后一个“status”是类名

我使用的是
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu导入
作为

temp = __import__ ("audit.status"), globals=(), locals=(), fromlist=["status"]

classname = temp.status 
它工作得很好,但是如何使用在下面语句中使用上述方法获取的modulename来获取函数名:

[obj代表getmembers(module_name)中的obj if isfunction(obj[1])]

如果您询问“如何找到模块对象的所有可调用属性?”


将模块名传递给getmembers()时,我出错了……因为它给了我空的函数列表。我不知道什么是
getmembers
,但您不需要使用它。我的答案在任何地方都不需要
getmembers
。我尝试了[method for method in dir(classname),如果可以调用(getattr(cname,method))],但这给了我所有属性的列表,比如“class”、“format”,还有类本身的方法以及父类的方法和属性。。。是否有一种方法可以过滤掉这些内容,从而只获取我提供的类名的方法确保提供的是模块对象而不是模块名,无论您在哪里用模块替换“math”。否则,您只需检查
字符串
类。在哪里定义了
getmembers
isfunction
?您是否有任何未向我们展示的
import
语句?请向我们展示您尚未展示的所有导入声明。从inspect import getmembers,IsFunction请编辑您的问题,而不是在评论中回答我不是在评论中回答…我的问题非常清楚…我只是偶然地回答这些ppls合作,
inspect.isfunction
对于内置函数和类方法都返回False。这在文档中并不完全明显。
>>> import math
>>> attrs = [getattr(math, name) for name in dir(math)]
>>> callables = [attr for attr in attrs if hasattr(attr, "__call__")]
>>> print callables
[<built-in function acos>, <built-in function acosh>, <built-in function asin>,
...
<built-in function tan>, <built-in function tanh>, <built-in function trunc>]
>>> names = [name for name in dir(math) if hasattr(getattr(math, name), "__call__")]
>>> print names
['acos', 'acosh', 'asin', ... 'tan', 'tanh', 'trunc']