蟒蛇inspect.getargspec具有内置功能 [英] Python inspect.getargspec with built-in function
问题描述
我试图找出从模块获取的方法的参数。
我发现了一个检查
模块,一个方便的功能, getargspec
。
它适用于我定义一个函数,但是只适用于功能从导入模块不能正常工作。
I'm trying to figure out the arguments of a method retrieved from a module.
I found an inspect
module with a handy function, getargspec
.
It works for a function that I define, but won't work for functions from an imported module.
import math, inspect
def foobar(a,b=11): pass
inspect.getargspec(foobar) # this works
inspect.getargspec(math.sin) # this doesn't
我会得到一个错误这样的:
I'll get an error like this:
File "C:\...\Python 2.5\Lib\inspect.py", line 743, in getargspec
raise TypeError('arg is not a Python function')
TypeError: arg is not a Python function
是 inspect.getargspec
仅用于本地功能还是我做错了什么设计?
Is inspect.getargspec
designed only for local functions or am I doing something wrong?
推荐答案
这是不可能得到这种对于在C,而不是Python实现一个功能的信息。
It is impossible to get this kind of information for a function that is implemented in C instead of Python.
这样做的原因是,有没有办法找出什么参数的方法接受除通过解析(自由格式)文档字符串,因为参数的(有点)GETARG样的方式传递 - 也就是说,它是不可能找到出它接受什么样的参数,而不实际执行的功能。
The reason for this is that there is no way to find out what arguments the method accepts except by parsing the (free-form) docstring since arguments are passed in a (somewhat) getarg-like way - i.e. it's impossible to find out what arguments it accepts without actually executing the function.
这篇关于蟒蛇inspect.getargspec具有内置功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!