蟒蛇inspect.getargspec具有内置功能 [英] Python inspect.getargspec with built-in function

查看:1330
本文介绍了蟒蛇inspect.getargspec具有内置功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出从模块获取的方法的参数。
我发现了一个检查模块,一个方便的功能, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆