Proper way to use kwargs in Python
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Python, **kwargs allows a function to accept any number of keyword arguments as a dictionary. The name kwargs is a convention — the ** syntax is what matters. This feature is essential for writing flexible APIs, forwarding arguments to other functions, decorators, and class inheritance. Understanding when and how to use **kwargs properly prevents common bugs like passing unexpected arguments or losing type safety.
Basic Usage
**kwargs collects all keyword arguments not matched by named parameters into a dictionary. Inside the function, access values using kwargs.get(key, default) or kwargs[key].
Combining with Positional and Regular Keyword Args
The parameter order must be: positional arguments, *args, keyword arguments, **kwargs.
Forwarding Arguments to Another Function
Use **kwargs to forward keyword arguments to downstream functions without explicitly listing every parameter. This is the most common and powerful use case.
Using kwargs in Decorators
Decorators use *args, **kwargs to accept and forward any arguments to the wrapped function without knowing its signature in advance.
Using kwargs in Class Inheritance
Using **kwargs with super().__init__() enables cooperative multiple inheritance by passing unused arguments up the method resolution order (MRO).
Unpacking a Dictionary as Keyword Arguments
The ** operator unpacks a dictionary into keyword arguments at the call site. This is the inverse of **kwargs in function definitions.
Validating kwargs
Since **kwargs accepts anything, validate keys explicitly to catch typos and invalid arguments early.
Common Pitfalls
- Accepting
**kwargswhen explicit parameters are better: Using**kwargshides the function's expected arguments from callers, IDE autocompletion, and type checkers. Only use**kwargswhen the set of arguments is genuinely dynamic or for forwarding — prefer named parameters for known arguments. - Forgetting to forward
**kwargsin decorators or inheritance: If a decorator wrapper uses*args, **kwargsbut forgets to pass**kwargsto the wrapped function, keyword arguments are silently dropped. Always callfunc(*args, **kwargs)inside the wrapper. - Mutating kwargs directly: Modifying the
kwargsdictionary (e.g.,kwargs.pop("key")) changes the dict in place. If the caller reuses the dictionary, they see the modification. Usekwargs.get()or make a copy first. - Name collision between explicit params and kwargs: If a function has
def f(name, **kwargs)and the caller passesnameboth positionally and as a keyword in a dict, Python raisesTypeError: got multiple values for argument. Validate or separate the namespaces. - Not validating kwargs keys: Since
**kwargsaccepts any key, typos in argument names are silently ignored rather than raising errors. Always validate against a set of expected keys or use explicit parameters.
Summary
**kwargscollects extra keyword arguments into a dictionary- Parameter order: positional,
*args, keyword-only,**kwargs - Primary use cases: argument forwarding, decorators, and cooperative inheritance
- Use
**dictto unpack a dictionary into keyword arguments at the call site - Validate kwargs keys explicitly to catch typos and unexpected arguments
- Prefer named parameters over
**kwargswhen the argument set is known
Related reading
.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.