Use of *args and **kwargs
In Python, *args and **kwargs are special keywords that allow you to pass a variable number of arguments to a function.
In Python, *args and **kwargs are special keywords that allow you to pass a variable number of arguments to a function.
*args is used to pass a non-keyworded, variable-length argument list to a function. For example:
*Pass a non-keyworded, variable-length argument list to a function in Python using args
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Python - The Practical Guide</div>
**kwargs is used to pass a keyworded, variable-length argument list to a function. For example:
**Pass a keyworded, variable-length argument list to a function in Python using kwargs
Both *args and **kwargs can be used to pass arguments to a function in a flexible way, allowing you to pass a varying number of arguments to a function.
You can also use both *args and **kwargs in the same function definition, but *args must come before **kwargs.
For example:
**Use *args and kwargs in one function in Python
This would print:
1
2
3
name = Alice
age = 20