Python *args and **kwargs
Posted in Python by Dirk - last update: Feb 14, 2024
What are *args
In Python, *args
is a syntax that allows a function to accept a variable number of positional arguments. The asterisk (*
) is used in the function definition to collect any additional positional arguments passed to the function into a tuple. The name args
is a convention, you can use any valid variable name preceded by the asterisk.
*args example
def print_arguments(*args):
for arg in args:
print(arg)
# Using the function with different numbers of arguments
print_arguments(1, 2, 3) # Output: 1 2 3
print_arguments("a", "b") # Output: a b
print_arguments("x", 10, True) # Output: x 10 True
In this example, the print_arguments
function accepts any number of positional arguments, and the *args
syntax collects them into the args
tuple. The function then prints each argument.
As mentioned before, it’s important to note that the name args is a convention, and you could use any other valid variable name preceded by the asterisk. For example, you could use *values
or *items
instead of *args
. The asterisk (*
) indicates that the function can accept a variable number of arguments, and the actual variable name is up to the programmer’s preference.
What are **kwargs
In Python, **kwargs
is a syntax that allows a function to accept a variable number of keyword arguments. Similar to *args
, the double asterisk (**
) is used in the function definition to collect any additional keyword arguments passed to the function into a dictionary. The name kwargs
is a convention, but like *args
, you can use any valid variable name preceded by the double asterisk.
**kwargs example:
def print_key_value_pairs(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
# Using the function with different keyword arguments
print_key_value_pairs(name="Alice", age=25, city="Wonderland")
In this example, the print_key_value_pairs function accepts any number of keyword arguments, and the **kwargs
syntax collects them into the kwargs
dictionary. The function then prints each key-value pair.
output:
name: Alice
age: 25
city: Wonderland
It’s worth noting that you can use both *args
and **kwargs
in the same function definition if you want to create a function that accepts both positional and keyword arguments with variable lengths.
When to use *args and **kwargs
You use *args
and **kwargs
when you want to create functions that can accept a variable number of arguments, either positional or keyword arguments, respectively. These features provide flexibility in function definitions, allowing you to handle different scenarios where the number of arguments may vary.
Combined *args and **kwargs example
You can use both *args
and **kwargs
in the same function definition when you need to handle both positional and keyword arguments with variable lengths.
def process_data(*args, **kwargs):
# Code to process positional arguments in args
for arg in args:
# process arg
# Code to process keyword arguments in kwargs
for key, value in kwargs.items():
# process key-value pair
Other articles