What are *args and **kwargs
In Python, *args
and **kwargs
are commonly used terms that are used to pass a variable number of arguments to a function. Both *args
and **kwargs
are special syntaxes that allow you to pass a variable number of arguments to a function, without explicitly defining them in the function definition.
*args
is short for "arguments" and is used to pass a variable number of non-keyword arguments to a function. On the other hand, **kwargs
is short for "keyword arguments" and is used to pass a variable number of keyword arguments to a function.
Using *args
and **kwargs
can be very useful when you want to create a flexible function that can handle different numbers of arguments or keyword arguments.
How to Use *args
To use *args
, simply include an asterisk before the parameter name in the function definition. This tells Python that any additional positional arguments should be collected into a tuple and passed into the function as the parameter value.
Here is an example of a function that accepts *args
:
def sum_numbers(*args):
total = 0
for number in args:
total += number
return total
In this example, the sum_numbers
function takes any number of arguments and sums them together. For instance, you can call this function with different numbers of arguments:
print(sum_numbers(1, 2, 3)) # Output: 6
print(sum_numbers(1, 2, 3, 4, 5)) # Output: 15
print(sum_numbers(2, 4, 6, 8, 10, 12)) # Output: 42
As you can see, *args
allows the function to handle a varying number of arguments without having to define each one explicitly in the function definition.
How to Use **kwargs
To use **kwargs
, you need to define a parameter with two asterisks (**
) in the function signature. This parameter will then receive a dictionary of all keyword arguments passed to the function.
Here's an example of how to use **kwargs
in Python:
def my_function(\*\*kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
my_function(name="John", age=30, city="New York")
In this example, we define a function my_function
that takes a variable number of keyword arguments using the **kwargs
syntax. The function then loops over the dictionary of keyword arguments using the .items()
method, printing out each key-value pair.
When we call the function with my_function(name="John", age=30, city="New York")
, the output will be:
name: John
age: 30
city: New York
You can also pass a dictionary to a function that uses kwargs by using the double asterisk () syntax to unpack the dictionary. Here's an example:
def my_function(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
my_dict = {"name": "John", "age": 30, "city": "New York"}
my_function(**my_dict)
In this example, we define a dictionary my_dict
with three key-value pairs. We then call the my_function
with the double asterisk (**
) syntax to unpack the dictionary and pass its contents as keyword arguments to the function.
When we run the code, we get the same output as before:
name: John
age: 30
city: New York