Python inspect.getargspec具有内置函数

Pee*_*tWc 6 python methods arguments function inspect

我试图弄清楚从模块中检索的方法的参数.我发现了一个inspect带有便利功能的模块getargspec.它适用于我定义的函数,但不适用于导入模块的函数.

import math, inspect
def foobar(a,b=11): pass
inspect.getargspec(foobar)  # this works
inspect.getargspec(math.sin) # this doesn't
Run Code Online (Sandbox Code Playgroud)

我会收到这样的错误:

   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
Run Code Online (Sandbox Code Playgroud)

inspect.getargspec设计为仅供本地功能还是我做错了什么?

Thi*_*ter 13

对于用C而不是Python实现的函数,不可能得到这种信息.

这样做的原因是没有办法找出方法接受的参数,除非通过解析(自由格式)文档字符串,因为参数是以(有点)类似getarg的方式传递的 - 即不可能找出什么参数它接受而不实际执行该功能.