Python *args / **kwargs
Learn all about Python *args / **kwargs in this comprehensive tutorial.
- •By default, a function must be called with the correct number of arguments.
- •If you do not know how many arguments will be passed into your function, add a * before the parameter name.
- •The *args parameter allows a function to accept any number of positional arguments.
- •You can combine regular parameters with *args.
- •*args is useful when you want to create flexible functions:
- •If you do not know how many keyword arguments will be passed into your function, add two asterisks ** before the parameter name.
- •The **kwargs parameter allows a function to accept any number of keyword arguments.
- •You can combine regular parameters with **kwargs.
- •You can use both *args and **kwargs in the same function.
- •The * and ** operators can also be used when calling functions to unpack (expand) a list or dictionary into separate arguments.
*args and **kwargs
By default, a function must be called with the correct number of arguments.
However, sometimes you may not know how many arguments that will be passed into your function.
*args and **kwargs allow functions to accept a unknown number of arguments.
Arbitrary Arguments - *args
If you do not know how many arguments will be passed into your function, add a * before the parameter name.
This way, the function will receive a tuple of arguments and can access the items accordingly:
What is *args?
The *args parameter allows a function to accept any number of positional arguments.
Inside the function, args becomes a tuple containing all the passed arguments:
Using *args with Regular Arguments
You can combine regular parameters with *args.
Regular parameters must come before *args:
In this example, "Hello" is assigned to greeting, and the rest are collected in names.
Practical Example with *args
*args is useful when you want to create flexible functions:
Arbitrary Keyword Arguments - **kwargs
If you do not know how many keyword arguments will be passed into your function, add two asterisks ** before the parameter name.
This way, the function will receive a dictionary of arguments and can access the items accordingly:
What is **kwargs?
The **kwargs parameter allows a function to accept any number of keyword arguments.
Inside the function, kwargs becomes a dictionary containing all the keyword arguments:
Using **kwargs with Regular Arguments
You can combine regular parameters with **kwargs.
Regular parameters must come before **kwargs:
Combining *args and **kwargs
You can use both *args and **kwargs in the same function.
The order must be: regular parameters *args **kwargs
Unpacking Arguments
The * and ** operators can also be used when calling functions to unpack (expand) a list or dictionary into separate arguments.
If you have values stored in a list, you can use * to unpack them into individual arguments:
If you have keyword arguments stored in a dictionary, you can use ** to unpack them:
Module quiz
2 questionsWhich of the following is true about Python *args / **kwargs?
What is the most common pitfall when working with Python *args / **kwargs?
Answer all questions to submit.