How to get a list of parameter names inside Python function?

PythonServer Side ProgrammingProgramming

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 a Method and foo.

Example

import inspect
def aMethod(arg1, arg2): pass
print(inspect.getargspec(aMethod))
def foo(a,b,c=4, *arglist, **keywords): pass
print(inspect.getargspec(foo))

 Output

ArgSpec(args=['arg1', 'arg2'], varargs=None, keywords=None, defaults=None)
ArgSpec(args=['a', 'b', 'c'], varargs='arglist', keywords='keywords', defaults=(4,))
raja
Updated on 18-Oct-2022 11:27:29

Advertisements