Get the number of Explicit Arguments in the Init of a Class
Last Updated :
23 Jul, 2025
In Python, the __init__ method is used for initializing a newly created object. It typically contains parameters that set the initial state of an object. To count the number of explicit arguments in the __init__
method of a class, we can use the inspect
module from Python's standard library. In this article, we will see how we can get the number of explicit arguments in the init of a Python Class.
Using the Inspect Module
The inspect module provides several useful functions to get information about live objects, including classes, methods, and functions.
In this example, we use inspect.signature() function to get the signature of the __init__ method and count the parameters, excluding self.
Python
import inspect
class ExampleClass:
def __init__(self, arg1, arg2, arg3):
pass
def count_init_args(cls):
init_method = cls.__init__
if not callable(init_method):
return 0
init_signature = inspect.signature(init_method)
# Exclude 'self' and return the count of parameters
return len(init_signature.parameters) - 1
num_args = count_init_args(ExampleClass)
print(f"Number of explicit arguments in __init__: {num_args}")
Output:
Number of explicit arguments in __init__: 3
Analyzing the Source Code
Another approach is to analyze the source code of the __init__ method using the inspect module. This method is useful when you want to include the analysis of decorators or other complexities.
Here, we retrieve the source code of the __init__ method and parse it to count the arguments. The sig.parameters.values()
returns a dictionary of parameters, and the list comprehension filters out the self
parameter. This approach provides more control over how the method signature is interpreted.
Python
import inspect
class AnotherClass:
def __init__(self, x, y, z):
pass
def count_init_args_from_source(cls):
init_method = cls.__init__
# Get the signature of the __init__ method
sig = inspect.signature(init_method)
# Get the parameters from the signature, excluding 'self'
params = [p for p in sig.parameters.values() if p.name != 'self']
return len(params)
num_args = count_init_args_from_source(AnotherClass)
print(f"Number of explicit arguments in __init__: {num_args}")
Output
Number of explicit arguments in __init__: 3
Using Function Annotations
If the class uses function annotations, we can leverage them to count the number of arguments. This is a more advanced technique that can be useful in specific scenarios.
In this example, we use function annotations to determine the number of arguments. Note that this method requires that the arguments are annotated, which might not always be the case. The __annotation__ retrieves the annotations of the __init__
method. The annotations are stored in a dictionary where the keys are the parameter names and the values are the types.
Python
class AnnotatedClass:
def __init__(self, a: int, b: str, c: float):
pass
def count_annotated_init_args(cls):
init_method = cls.__init__
annotations = init_method.__annotations__
# Exclude 'return' annotation and count the remaining ones
return len(annotations)
num_args = count_annotated_init_args(AnnotatedClass)
print(f"Number of explicit arguments in __init__: {num_args}")
Output:
Number of explicit arguments in __init__: 3
Conclusion
Determining the number of explicit arguments in the __init__ method of a class can be achieved using various techniques. The inspect module provides robust tools for this purpose, whether by examining the method signature directly, analyzing the source code, or using function annotations. Each method has its strengths and can be chosen based on the specific requirements of your project. By understanding these approaches, you can make your Python code more flexible and introspective.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice