[英]How to extract signature from function reference in Python?
Let's say I have a function in Python like so: 假设我在Python中有一个函数,如下所示:
def foo(x): pass
According to Python, 'foo' alone is a function reference, right? 根据Python,'foo'单独是一个函数引用,对吧?
>>> def foo(x): pass
...
>>> foo
<function foo at 0xb7f3d1b4>
Is there any way I can examine the function reference to determine the number of arguments it expects? 有什么办法可以检查函数引用来确定它所期望的参数数量吗?
You need inspect.getfullargspec
in py3k or inspect.getargspec
in earlier versions. 您需要
inspect.getfullargspec
在py3k或inspect.getargspec
早期版本。
>>> def foo(x): pass
>>> import inspect
>>> inspect.getfullargspec(foo)
FullArgSpec(args=['x'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.