How to get method parameter names?
Take a look at the
inspect module - this will do the inspection of the various code object properties for you.
'arg1', 'arg2'], None, None, None)inspect.getfullargspec(a_method)([
The other results are the name of the *args and **kwargs variables, and the defaults provided. ie.
def foo(a, b, c=4, *arglist, **keywords): passinspect.getfullargspec(foo)(['a', 'b', 'c'], 'arglist', 'keywords', (4,))
Note that some callables may not be introspectable in certain implementations of Python. For Example, in CPython, some built-in functions defined in C provide no metadata about their arguments. As a result, you will get a
ValueError if you use
inspect.getfullargspec() on a built-in function.
Since Python 3.3, you can use
inspect.signature() to see the call signature of a callable object:
4, *arglist, **keywords)>inspect.signature(foo)<Signature (a, b, c=
In CPython, the number of arguments is
and their names are in the beginning of
These are implementation details of CPython, so this probably does not work in other implementations of Python, such as IronPython and Jython.
One portable way to admit "pass-through" arguments is to define your function with the signature
func(*args, **kwargs). This is used a lot in e.g. matplotlib, where the outer API layer passes lots of keyword arguments to the lower-level API.