The Wayback Machine - https://web.archive.org/web/20250120112859/https://www.geeksforgeeks.org/how-to-get-list-of-parameters-name-from-a-function-in-python/
Open In App

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

Last Updated : 02 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

Python
# 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.

Python
# 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.

Python
# 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.

Python
# 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.

Python
# 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.

Python
# import required modules 
import inspect 

# use getargspec() 
print(inspect.getargspec(len)) 

Output:

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

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

What is the parameter list of a function?

The parameter list of a function consists of the variables that the function takes as inputs when it is called. These parameters can be defined in the function’s definition and can include positional parameters, default parameters, variable-length parameters (using *args and **kwargs), and keyword-only parameters.

For example:

def example_function(param1, param2, *args, **kwargs):
pass

How to get a list of variables in Python?

To get a list of variables (names) defined in the current scope, you can use the locals() function for the local scope or globals() for the global scope. These functions return a dictionary of variable names and their values.

For example:

a = 10
b = 20
print(locals().keys()) # Get local variables
print(globals().keys()) # Get global variables

How to return a list from a function in Python?

To return a list from a function in Python, simply create the list and use the return statement to return it.

For example:

def create_list():
my_list = [1, 2, 3, 4, 5]
return my_list

result = create_list()
print(result)

How to get the function name in Python?

To get the name of a function, you can use the __name__ attribute of the function object.

For example:

def my_function():
pass

print(my_function.__name__)

How to access variable names?

To access variable names in the current scope, you can use the locals() or globals() functions, which return dictionaries containing the current local and global variables, respectively.

For example:

x = 5
y = "hello"

# Access variable names in the local scope
local_vars = locals()
print(local_vars)

# Access variable names in the global scope
global_vars = globals()
print(global_vars)


Next Article

Similar Reads

three90RightbarBannerImg