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.
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.
Output:
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.
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.
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.
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.
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.
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.
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).parametersto get parameter names. - '
inspect.signatureis 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.wrapsso introspection still works correctly.
Related reading
- Getting No loop matching the specified signature and casting error
- Getting number of elements in an iterator in Python
- Getting one value from a tuple
- Getting pika.exceptions.StreamLostError Transport indicated EOF while running python script docker image which using pika
- Getting rid of n when using .readlines
- Getting S3 objects' last modified datetimes with boto
- Getting started with secure AWS CloudFront streaming with Python
- Getting tensorflow is not a supported wheel on this platform
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.