Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get method parameter names?

Given the Python function:

def a_method(arg1, arg2):     pass 

How can I extract the number and names of the arguments. I.e., given that I have a reference to func, I want the func.[something] to return ("arg1", "arg2").

The usage scenario for this is that I have a decorator, and I wish to use the method arguments in the same order that they appear for the actual function as a key. I.e., how would the decorator look that printed "a,b" when I call a_method("a", "b")?

like image 647
Staale Avatar asked Oct 20 '08 14:10

Staale


People also ask

Which method is used to get all the parameter names?

getName : Returns the name of the parameter. If the parameter's name is present, then this method returns the name provided by the . class file. Otherwise, this method synthesizes a name of the form argN , where N is the index of the parameter in the descriptor of the method that declares the parameter.

How get arguments name in Python?

To extract the number and names of the arguments from a function or function[something] to return ("arg1", "arg2"), we use the inspect module. The given code is written as follows using inspect module to find the parameters inside the functions aMethod and foo.

How do you get parameters in Java?

To get all request parameters in java, we get all the request parameter names and store it in an Enumeration object. Our Enumeration object now contains all the parameter names of the request. We then iterate the enumeration and get the value of the request given the parameter name.

Which method is used to fetch the parameter types using method parameter reflection?

reflect package is used to fetch the parameter types using method parameter reflection. Reflection is a process of analyzing and modifying all capabilities of class at runtime.


2 Answers

Take a look at the inspect module - this will do the inspection of the various code object properties for you.

>>> inspect.getfullargspec(a_method) (['arg1', 'arg2'], None, None, None) 

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): pass >>> inspect.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:

>>> inspect.signature(foo) <Signature (a, b, c=4, *arglist, **keywords)> 
like image 142
Brian Avatar answered Oct 05 '22 23:10

Brian


In CPython, the number of arguments is

a_method.func_code.co_argcount 

and their names are in the beginning of

a_method.func_code.co_varnames 

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.

like image 23
Jouni K. Seppänen Avatar answered Oct 05 '22 23:10

Jouni K. Seppänen