In this article, we are going to discuss how to get list parameters from a function in Python. The inspect module helps in checking the objects present in the code that we have written. We are going to use two methods i.e. signature() and getargspec() methods from the inspect module to get the list of parameters name of function or method passed as an argument in one of the methods.
Using inspect.signature() method
Below are some programs which depict how to use the signature() method of the inspect module to get the list of parameters name:
Example 1: Getting the parameter list of a method.
Python3
import inspect
import collections
print(inspect.signature(collections.Counter))
Output:
(*args, **kwds)
Example 2: Getting the parameter list of an explicit function.
Python3
def fun(a, b):
return a**b
import inspect
print(inspect.signature(fun))
Output:
(a, b)
Example 3: Getting the parameter list of an in-built function.
Python3
import inspect
print(inspect.signature(len))
Output:
(obj, /) Using inspect.getargspec() method
Below are some programs which depict how to use the getargspec() method of the inspect module to get the list of parameters name:
Example 1: Getting the parameter list of a method.
Python3
import inspect
import collections
print(inspect.getargspec(collections.Counter))
Output:
ArgSpec(args=[], varargs=’args’, keywords=’kwds’, defaults=None)
Example 2: Getting the parameter list of an explicit function.
Python3
def fun(a, b):
return a**b
import inspect
print(inspect.getargspec(fun))
Output:
ArgSpec(args=[‘a’, ‘b’], varargs=None, keywords=None, defaults=None)
Example 3: Getting the parameter list of an in-built function.
Python3
import inspect
print(inspect.getargspec(len))
Output:
ArgSpec(args=[‘obj’], varargs=None, keywords=None, defaults=None)
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Last Updated :
29 Dec, 2020
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...