Python
function parameters
programming
code duplication
Python tips

Getting list of parameter names inside python function

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

If you want a function's parameter names in Python, the standard answer is inspect.signature. It gives you the declared parameters of a function object, which is usually what people mean when they ask for parameter names.

The Best Modern Tool: inspect.signature

The inspect module can introspect live Python objects, including functions.

python
1import inspect
2
3
4def example(a, b, c=10, *args, d=None, **kwargs):
5    pass
6
7
8sig = inspect.signature(example)
9param_names = list(sig.parameters.keys())
10print(param_names)

Output:

text
['a', 'b', 'c', 'args', 'd', 'kwargs']

This is the most direct and readable way to get the declared parameter names.

Understanding What You Are Getting

inspect.signature gives you metadata about the function definition, not about the current values passed by the caller.

For example, each parameter has:

  • a name
  • a kind, such as positional or keyword-only
  • an optional default value
  • an optional annotation

You can inspect that information too.

python
1import inspect
2
3
4def example(a, b=5, *, debug=False):
5    pass
6
7
8for name, param in inspect.signature(example).parameters.items():
9    print(name, param.kind, param.default)

This is useful if you need more than just the raw names.

If You Only Need Positional Argument Names

Sometimes you only want the ordinary named parameters and do not want *args or **kwargs in the result. Filter by parameter kind.

python
1import inspect
2
3
4def example(a, b, *args, c=3, **kwargs):
5    pass
6
7
8sig = inspect.signature(example)
9names = [
10    name
11    for name, param in sig.parameters.items()
12    if param.kind in (
13        inspect.Parameter.POSITIONAL_ONLY,
14        inspect.Parameter.POSITIONAL_OR_KEYWORD,
15        inspect.Parameter.KEYWORD_ONLY,
16    )
17]
18print(names)

This helps when you want a user-facing list of meaningful named parameters rather than every possible binding form.

Inside The Function Versus Outside The Function

The title often causes a subtle misunderstanding. If you are inside a function and want the names from the function definition, you still need the function object.

python
1import inspect
2
3
4def demo(x, y, z=3):
5    print(list(inspect.signature(demo).parameters.keys()))
6
7
8demo(1, 2)

That prints the names declared by demo.

But if what you really want is a mapping of names to the current call's local values, that is a different task. In that case, locals() is relevant.

python
1def demo(x, y, z=3):
2    print(locals())
3
4
5demo(1, 2)

That prints the current local variable mapping, not just the declared parameter list.

Older Introspection APIs

You may also see older APIs such as inspect.getfullargspec.

python
1import inspect
2
3
4def example(a, b=2, *args, **kwargs):
5    pass
6
7
8spec = inspect.getfullargspec(example)
9print(spec.args)
10print(spec.varargs)
11print(spec.varkw)

This still works, but inspect.signature is the cleaner modern interface for most code.

Decorators Can Affect Introspection

If a function is wrapped by a decorator that does not preserve metadata, introspection may show the wrapper's parameters rather than the original function's.

A good decorator uses functools.wraps.

python
1import functools
2import inspect
3
4
5def traced(fn):
6    @functools.wraps(fn)
7    def wrapper(*args, **kwargs):
8        return fn(*args, **kwargs)
9    return wrapper
10
11
12@traced
13def greet(name, punctuation="!"):
14    pass
15
16
17print(list(inspect.signature(greet).parameters.keys()))

Without functools.wraps, introspection becomes much less useful.

Common Pitfalls

The most common mistake is confusing parameter names with runtime argument values. inspect.signature gives you the definition, not the actual call data.

Another mistake is introspecting a decorated wrapper instead of the original function. Use functools.wraps in decorators to preserve metadata.

Developers also sometimes use old introspection helpers when inspect.signature would be simpler and clearer.

Finally, if you want the current function's local variable values, use locals() rather than trying to reconstruct them from the signature alone.

Summary

  • Use inspect.signature(function).parameters to get parameter names.
  • 'inspect.signature is the modern standard answer in Python.'
  • Filter parameter kinds if you want only certain categories of parameters.
  • Use locals() when you want current runtime values instead of declared names.
  • Decorators should use functools.wraps so introspection still works correctly.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.