What is kwargs in python
Last updated: April 1, 2026
Key Facts
- **kwargs stands for 'keyword arguments' and enables functions to receive any number of named parameters
- Arguments passed to **kwargs are collected into a dictionary that can be accessed and iterated within the function
- Functions can combine regular parameters, *args (positional), and **kwargs (keyword) in the same definition
- The name 'kwargs' is a convention—you can use other names, but **kwargs is the standard Python idiom
- Commonly used in decorators, flexible APIs, and functions that need to pass arguments to other functions
Understanding **kwargs in Python
kwargs is a Python feature that allows functions to accept an arbitrary number of keyword arguments. The double asterisks (**) are a special syntax that signals to Python to collect named arguments into a dictionary.
How **kwargs Works
When you define a function with **kwargs, any keyword arguments passed to the function are collected into a dictionary. Inside the function, you access these arguments like any other dictionary. For example:
- Function definition: def my_function(**kwargs)
- Function call: my_function(name='John', age=30, city='New York')
- Inside function: kwargs becomes {'name': 'John', 'age': 30, 'city': 'New York'}
Basic Example
The name 'kwargs' is a Python convention, though technically you could use any name after the double asterisks. However, using **kwargs is standard practice and makes code more readable. The double asterisks are what matter syntactically; they tell Python to expect keyword arguments.
Combining with Other Parameters
Functions can use regular positional arguments, *args (arbitrary positional arguments), and **kwargs (arbitrary keyword arguments) together. The order matters: regular parameters first, then *args, then **kwargs. Example: def function(a, b, *args, **kwargs). This flexibility makes functions highly adaptable to different calling conventions.
Common Use Cases
kwargs is particularly useful in several scenarios:
- Creating decorators that need to pass arguments to wrapped functions
- Building APIs that accept varying numbers of optional parameters
- Creating wrapper functions that delegate to other functions
- Configuration functions that accept dynamic key-value pairs
- Functions that need to be flexible about which parameters they accept
Iterating Through kwargs
Since **kwargs creates a dictionary, you can iterate through key-value pairs using .items(). This is useful when you need to process all passed arguments dynamically without knowing their names in advance. This flexibility is one of the most powerful aspects of using **kwargs.
Best Practices
While **kwargs provides flexibility, use it judiciously. Over-relying on **kwargs can make function signatures unclear and code harder to understand. Document what keyword arguments your function expects, and consider using explicit parameters for commonly used arguments.
Related Questions
What is the difference between *args and **kwargs?
*args collects positional arguments into a tuple, while **kwargs collects keyword arguments into a dictionary. *args comes before **kwargs in function definitions.
Can you use kwargs without the double asterisks?
No, the double asterisks (**) are required syntax. They tell Python to collect keyword arguments. The name 'kwargs' is just a convention; you could use **options or **params, but the double asterisks are essential.
How do you pass kwargs to another function?
You can pass **kwargs to another function by using the ** unpacking operator again. For example: other_function(**kwargs) will unpack the dictionary back into keyword arguments.
More What Is in History
Also in History
- Who Is Nikola Tesla
- Why is sipping a beverage with the little finger raised associated with the aristocracy--or upper-class pretensions
- Who was Alexander before Alexander
- How do I make sense of the dates of the Trojan War vs the dates of "Sparta"
- What does ad mean in history
- Is it possible for a writing to survive in poland after the fall of soviet union
- What does awkward mean