How to find the number of arguments in a Python function? (original) (raw)
Last Updated : 04 Mar, 2025
Finding the number of arguments in a Python function means checking how many inputs a function takes. **For example, in def my_function(a, b, c=10): pass, the total number of arguments is 3. Some methods also count special arguments like *args and **kwargs, while others only count fixed ones.
Using inspect.signature()
**inspect.signature() method provides a structured way to analyze a function’s parameters. It retrieves the function signature, which includes all positional, keyword, and default arguments. This makes it one of the most accurate ways to determine the number of arguments.
Python `
import inspect
def func(a, b, c=10): pass
sig = inspect.signature(func) num_args = len(sig.parameters)
print(num_args)
`
**Explanation:
- **inspect.signature() method retrieves the function signature.
- **sig.parameters returns a dictionary of all function parameters.
- **len(sig.parameters) counts the number of arguments, including default values but excluding *args and **kwargs.
Using inspect.getfullargspec()
inspect.getfullargspec() method extracts detailed information about a function’s arguments. It works similarly to inspect.signature(), but it is particularly useful for older Python versions . It can retrieve positional arguments, default values, *args, and **kwargs.
Python `
import inspect
def func(a, b, c=10, *args, **kwargs): pass
argspec = inspect.getfullargspec(func) num_args = len(argspec.args)
print(num_args)
`
**Explanation:
- **inspect.getfullargspec() extracts all argument details, including positional and keyword arguments.
- **argspec.args contains only standard positional arguments.
- **len(argspec.args) counts them, excluding *args and **kwargs.
Using __code__.co_argcount
**__code__.co_argcount method provides a quick way to count only positional arguments in a function. It directly accesses the function’s bytecode, making it the fastest method. However, it does not include keyword-only arguments (**kwargs) or variable-length arguments (*args).
Python `
def func(a, b, c=10, *args, **kwargs): pass
num_args = func.code.co_argcount print(num_args)
`
**Explanation:
- **__code__ refers to the function’s compiled bytecode.
- **co_argcount counts only the positional arguments (excluding *args and **kwargs).
Using len(*args) (For dynamic function calls)
**len(*args) approach is useful when counting arguments at runtime. It works by passing all arguments through *args and then using the len() function to count them. This method is best suited for functions that accept a variable number of arguments.
Python `
def no_of_argu(*args):
# Using len() to count arguments
return len(args)
a = 1 b = 3
Arguments passed: (1, 2, 4, a) → (1, 2, 4, 1)
n = no_of_argu(1, 2, 4, a)
Output
print(n)
`
**Explanation:
- **function uses *args, which allows an arbitrary number of arguments.
- **len(args) function counts how many arguments were passed when calling the function.