How can I find the number of arguments of a Python function?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you want to know how many arguments a Python function accepts, the best tool is usually inspect.signature. It gives you the full parameter list, including positional parameters, keyword-only parameters, defaults, and variable argument collectors such as *args and **kwargs. That matters because “number of arguments” can mean several different things.
For example, a function may have three declared parameters but only one required argument because the others have defaults. It may also accept an unlimited number of extra arguments. So the first step is deciding what count you actually want.
Use inspect.signature First
The inspect module is the most reliable and readable starting point for introspection.
The Signature.parameters mapping gives you each parameter and its kind. That is more useful than a raw count because it shows how the function is meant to be called.
If you only want the number of declared parameters:
That count includes positional, keyword-only, and defaulted parameters.
Count Only Required Arguments
Sometimes you do not want every declared parameter. You only want the number of required ones.
This returns 1 for the example above because only name is required.
That is often the more meaningful answer when validating call sites or generating documentation.
Understand *args and **kwargs
A function can declare collectors for extra positional or keyword arguments. These count as parameters, but they do not represent a fixed number of arguments.
inspect.signature(debug) will show three parameters, but the function can accept far more than three arguments at call time.
This is why a single integer answer can be misleading. Python call signatures are richer than a simple positional count.
__code__.co_argcount Is Narrower
You may see examples that use the function's code object.
This works for some simple cases, but it only reports certain positional parameters. It does not describe the whole calling interface as clearly as inspect.signature.
For modern code, prefer inspect.signature unless you have a very specific low-level reason to inspect the code object directly.
Decorators and Bound Methods Need Care
Introspection can get trickier with wrappers and methods.
If a decorator does not preserve metadata, the reported signature may describe the wrapper rather than the original function. Using functools.wraps helps preserve the original signature information.
For bound methods, remember that self is part of the function definition even though callers do not pass it explicitly.
The bound method view is often closer to what callers care about.
Common Pitfalls
The most common mistake is assuming there is only one meaningful “argument count.” Required arguments, declared parameters, and accepted runtime arguments are not always the same thing.
Another issue is using __code__.co_argcount and forgetting that it does not fully represent keyword-only parameters or flexible argument collectors.
A third problem is introspecting decorated functions that did not preserve metadata, which can produce a misleading signature.
Summary
- Use
inspect.signatureas the main way to inspect Python function arguments. - Decide whether you want total declared parameters or only required arguments.
- '
*argsand**kwargsmean a function may accept a variable number of call-time arguments.' - '
__code__.co_argcountis narrower and less expressive thaninspect.signature.' - Be careful with decorators and methods when interpreting signatures.

