🧠Second Brain
Search
Python `*args` and `**kwargs`
*args
and **kwargs
are special syntax in Python that allow you to pass a variable number of arguments to a function.
You would use *args
and **kwargs
when you don’t know how many arguments the function needs to accept at the time of writing the function. This is useful when you are writing a function that can be used in many different contexts, where the number and type of arguments may vary.
arg and kwargs are just names
Note thatÂargs
 is just a name. You’re not required to use the nameÂargs
. You can choose any name that you prefer.
# How they work
*args
allows you to pass a variable number of positional arguments to a function. The *
before args
indicates that all the positional arguments are collected and packed into a tuple. Here is an example:
|
|
In this example, my_func()
takes any number of positional arguments and prints them out. When the function is called with my_func(1, 2, 3)
, the output would be:
1 2 3
**kwargs
allows you to pass a variable number of keyword arguments to a function. The **
before kwargs
indicates that all the keyword arguments are collected and packed into a dictionary. Here is an example:
|
|
In this example, my_func()
takes any number of keyword arguments and prints out their keys and values. When the function is called with my_func(name='John', age=30, city='New York')
, the output would be:
|
|
# Using them together
You can also use *args
and **kwargs
together in the same function to accept any number of positional and keyword arguments. Here is an example:
|
|
In this example, my_func()
takes any number of positional and keyword arguments and prints them out. When the function is called with my_func(1, 2, 3, name='John', age=30, city='New York')
, the output would be:
|
|
# Cannot be defined implicit
No, *args
and **kwargs
cannot be used implicitly if they are not defined as parameters in the function signature.
If you try to pass arguments using *args
or **kwargs
to a function that doesn’t explicitly define them as parameters, you will get a TypeError
.
# Using standard parameters
If you combine it with standard arguments, the order is important.
- Standard arguments
*args
 arguments**kwargs
 arguments
For example, this function definition is correct:
|
|
Otherwise, you get a runtime error: ```
|
|
# Unpacking With the Asterisk Operators: * & **
There is much more you can do with Asterisk operators, see Python args and kwargs: Demystified – Real Python.
# Special case, using args and kwargs
However, you can still access the additional arguments using the args
and kwargs
variables that are available inside the function. For example:
pythonCopy code
|
|
In this updated function, *args
and **kwargs
are defined as parameters in the function signature. When the function is called with my_func(1, 2, 3, name='John', age=30)
, the output would be:
pythonCopy code
|
|
Here, args
is a tuple containing any additional positional arguments beyond the first two, and kwargs
is a dictionary containing any additional keyword arguments beyond the first two.
Positional argument order
Note again that the order of the parameters in the function signature matters.*args
must come after all the positional arguments, and**kwargs
must come after all the positional arguments and*args
.
Origin:
Tweet
References:
Created 2023-03-03