Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract signature from function reference in Python?

Tags:

python

Let's say I have a function in Python like so:

def foo(x): pass

According to Python, 'foo' alone is a function reference, right?

>>> 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?

like image 233
Stephen Gross Avatar asked Aug 18 '26 04:08

Stephen Gross


1 Answers

You need inspect.getfullargspec in py3k or inspect.getargspec in earlier versions.

 >>> def foo(x): pass

>>> import inspect
>>> inspect.getfullargspec(foo)
FullArgSpec(args=['x'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={})
like image 162
SilentGhost Avatar answered Aug 20 '26 19:08

SilentGhost



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!