Open In App

How to get list of parameters name from a function in Python?

Last Updated : 29 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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 required modules
import inspect
import collections
  
# use signature()
print(inspect.signature(collections.Counter))


Output:

(*args, **kwds)

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

Python3




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


Output:

(a, b)

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

Python3




# import required modules 
import inspect 
  
# use signature() 
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 required modules
import inspect
import collections
  
# use getargspec()
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




# explicit function
def fun(a, b):
    return a**b
  
# import required modules 
import inspect 
  
# use getargspec() 
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 required modules 
import inspect 
  
# use getargspec() 
print(inspect.getargspec(len))


Output:

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



Previous Article
Next Article

Similar Reads

GET Request Query Parameters with Flask
In this article, we will learn how we can use the request object in a flask to GET Request Query Parameters with Flask that is passed to your routes using Python. As we know, Flask is a web development framework written in Python, and the flask is widely used as a web framework for creating APIs (Application Programming Interfaces). Or we can say i
4 min read
Python | Print the initials of a name with last name in full
Given a name, print the initials of a name(uppercase) with last name(with first alphabet in uppercase) written in full separated by dots. Examples: Input : geeks for geeks Output : G.F.Geeks Input : mohandas karamchand gandhi Output : M.K.Gandhi A naive approach of this will be to iterate for spaces and print the next letter after every space excep
3 min read
Python IMDbPY – Getting Person name from searched name
In this article we will see how we can get the person name from the searched list of name, we use search_name method to find all the related names.search_name method returns list and each element of list work as a dictionary i.e. they can be queried by giving the key of the data, here key will be name. Syntax : names[0]['name']Here names is the lis
2 min read
GUI application to search a country name from a given state or city name using Python
In these articles, we are going to write python scripts to search a country from a given state or city name and bind it with the GUI application. We will be using the GeoPy module. GeoPy modules make it easier to locate the coordinates of addresses, cities, countries, landmarks, and Zipcode. Before starting we need to install the GeoPy module, so l
2 min read
Python | How to get function name ?
One of the most prominent styles of coding is following the OOP paradigm. For this, nowadays, stress has been to write code with modularity, increase debugging, and create a more robust, reusable code. This all encouraged the use of different functions for different tasks, and hence we are bound to know certain hacks of functions. This article disc
3 min read
How to pass optional parameters to a function in Python?
In Python, when we define functions with default values for certain parameters, it is said to have its arguments set as an option for the user. Users can either pass their values or can pretend the function to use theirs default values which are specified. In this way, the user can call the function by either passing those optional parameters or ju
4 min read
Python - Run same function in parallel with different parameters
In this article, we will learn how to run the same function in parallel with different parameters. We can run the same function in parallel with different parameters using parallel processing. The number of tasks performed by the program can be increased by parallel processing, which decreases the total processing time. These assist in addressing l
3 min read
Python IMDbPY – Get each episode name of each season of the series
In this article we will see how we can get the name/title of each episode for each season of the series from the episodes info set. Each series have seasons and each season has multiple episodes i.e episode is the subset of season and season is the subset of series. We get the episodes by adding episodes info set to the series In order to get this
3 min read
Python IMDbPY – Get series name from the episode
In this article we will see how we can get the series name from the episode from the episodes info set. Each series have seasons and each season has multiple episodes i.e episode is the subset of season and season is the subset of series. We get the episodes details by adding episodes info set to the series. In order to get this we have to do the f
2 min read
Python script to get device vendor name from MAC Address
Prerequisite: Python Requests A MAC address is a unique identity of a network interface that is used to address the device in a Computer Network. What does MAC Address consist of? A MAC address is made up of 12 hexadecimal digits, 6 octets separated by ':'. There are 6 octets in a MAC address. In the first half, the manufacturer info is stored. How
2 min read
three90RightbarBannerImg