Skip to content
Related Articles

Related Articles

How to get list of parameters name from a function in Python?
  • Last Updated : 29 Dec, 2020

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# import required modules
import inspect
import collections
  
# use signature()
print(inspect.signature(collections.Counter))

chevron_right


Output:



(*args, **kwds)

Example 2: Getting the parameter list of an explicit function.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# explicit function
def fun(a, b):
    return a**b
  
# import required modules 
import inspect 
  
# use signature() 
print(inspect.signature(fun)) 

chevron_right


Output:

(a, b)

Example 3: Getting the parameter list of an in-built function.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import required modules 
import inspect 
  
# use signature() 
print(inspect.signature(len))

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# import required modules
import inspect
import collections
  
# use getargspec()
print(inspect.getargspec(collections.Counter))

chevron_right


Output:

ArgSpec(args=[], varargs=’args’, keywords=’kwds’, defaults=None)

Example 2: Getting the parameter list of an explicit function.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# explicit function
def fun(a, b):
    return a**b
  
# import required modules 
import inspect 
  
# use getargspec() 
print(inspect.getargspec(fun)) 

chevron_right


Output:

ArgSpec(args=[‘a’, ‘b’], varargs=None, keywords=None, defaults=None)

Example 3: Getting the parameter list of an in-built function.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import required modules 
import inspect 
  
# use getargspec() 
print(inspect.getargspec(len))

chevron_right


Output:

ArgSpec(args=[‘obj’], varargs=None, keywords=None, defaults=None)

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :