How to extract a signature from a function reference in Python?

Say I have a function in Python:

def foo(x): pass

According to Python, only "foo" is a function reference, right?

>>> def foo(x): pass
...
>>> foo
<function foo at 0xb7f3d1b4>

Is there any way to check the function reference to determine the number of expected arguments?

+3
source share
1 answer

You need it inspect.getfullargspecin py3k or inspect.getargspecin earlier versions.

 >>> def foo(x): pass

>>> import inspect
>>> inspect.getfullargspec(foo)
FullArgSpec(args=['x'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={})
+4
source

Source: https://habr.com/ru/post/1731918/


All Articles