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

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.

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,))
How to get a list of parameter names inside Python function?

Published on 30-Oct-2021 13:40:54

You may also like this!