Python
programming
function arguments
coding tips
Python functions

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.

python
1import inspect
2
3def greet(name, title="Engineer", *, excited=False):
4    return f"Hello, {title} {name}!"
5
6sig = inspect.signature(greet)
7print(sig)
8print(sig.parameters)

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:

python
param_count = len(inspect.signature(greet).parameters)
print(param_count)

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.

python
1import inspect
2
3def greet(name, title="Engineer", *, excited=False):
4    return f"Hello, {title} {name}!"
5
6required = [
7    p for p in inspect.signature(greet).parameters.values()
8    if p.default is inspect._empty
9    and p.kind not in (p.VAR_POSITIONAL, p.VAR_KEYWORD)
10]
11
12print(len(required))

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.

python
def debug(label, *args, **kwargs):
    print(label, args, kwargs)

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.

python
1def add(a, b=0, *args):
2    return a + b
3
4print(add.__code__.co_argcount)

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.

python
1class Greeter:
2    def hello(self, name):
3        return f"Hello, {name}"
4
5import inspect
6print(inspect.signature(Greeter.hello))
7print(inspect.signature(Greeter().hello))

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.signature as the main way to inspect Python function arguments.
  • Decide whether you want total declared parameters or only required arguments.
  • '*args and **kwargs mean a function may accept a variable number of call-time arguments.'
  • '__code__.co_argcount is narrower and less expressive than inspect.signature.'
  • Be careful with decorators and methods when interpreting signatures.

Course illustration
Course illustration

All Rights Reserved.