Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

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

I have following python functions

def aMethod(arg1, arg2): pass
def foo(a,b,c=4, *arglist, **keywords): pass

How do I find the list of parameters inside these functions?


1 Answer
Rajendra Dharmkar

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.

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,))
Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.