Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
How to get a list of parameter names inside Python function?
To get a list of parameter names inside a Python function, you can use the inspect module. This module provides several functions that let you examine the properties of Python objects, including function signatures, parameter names, and default values.
Using inspect.getfullargspec()
The inspect.getfullargspec() function returns a named tuple containing information about a function's parameters, including argument names, variable args, keyword args, and default values ?
Example
import inspect
def aMethod(arg1, arg2):
pass
print(inspect.getfullargspec(aMethod))
def foo(a, b, c=4, *arglist, **keywords):
pass
print(inspect.getfullargspec(foo))
The output of the above code is ?
FullArgSpec(args=['arg1', 'arg2'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={})
FullArgSpec(args=['a', 'b', 'c'], varargs='arglist', varkw='keywords', defaults=(4,), kwonlyargs=[], kwonlydefaults=None, annotations={})
Using inspect.signature()
The inspect.signature() function provides a cleaner, more modern approach. It returns a Signature object whose parameters attribute is an ordered dictionary of Parameter objects. You can extract just the names by iterating over these objects ?
Example
import inspect
def get_parameter_names(func):
"""Returns a list of parameter names for the given function."""
signature = inspect.signature(func)
return [param.name for param in signature.parameters.values()]
# Example usage
def my_function(a, b, c=1, *args, **kwargs):
pass
parameter_names = get_parameter_names(my_function)
print(parameter_names)
The output of the above code is ?
['a', 'b', 'c', 'args', 'kwargs']
The list includes all parameters defined in my_function − positional parameters a and b, the keyword parameter c with its default, and the variable-length argument lists args and kwargs.
Practical Use: Input Validation Decorator
You can combine parameter introspection with type annotations to build a decorator that validates function inputs automatically ?
Example
import inspect
def validate_input(func):
"""Decorator that validates argument types against annotations."""
def wrapper(*args, **kwargs):
signature = inspect.signature(func)
bound = signature.bind(*args, **kwargs)
bound.apply_defaults()
for name, value in bound.arguments.items():
annotation = signature.parameters[name].annotation
if annotation is not inspect.Parameter.empty:
if not isinstance(value, annotation):
raise TypeError(f"{name} must be {annotation.__name__}")
return func(*args, **kwargs)
return wrapper
@validate_input
def add(a: int, b: float, c: int = 1):
return a + b + c
result = add(1, 2.0, c=3)
print(result)
The output of the above code is ?
6.0
The decorator uses inspect.signature() to bind the incoming arguments to parameter names, applies defaults, and then checks each value against its type annotation before calling the original function.
Conclusion
The inspect module is the standard way to retrieve parameter names from Python functions. Use inspect.signature() for modern Python 3 code, as it provides a rich Signature object suitable for introspection, validation, and documentation tasks.
