Python Variable-length Variable List
A Python variable is created by assigning a value to a variable name.
Besides standard python variable, there are two special variable syntaxes
used in python to assign a variable-length variable list to a variable name. They are
*args and
**kwargs
Python *arg and **kwarg
Python
*arg and
**kwarg syntaxes are designed for representing a simple sequence and a keyword sequence.
*args
For
*args,
* is the syntax symbol to indicate a variable-length variable list.
args is the variable name of the variable list.
*args is used to represent a variable-length of non-keyworded argument list.
*args can also be used to pass a variable number of arguments to a function.
**kwargs
For
**kwargs:
** is the syntax symbol to indicate a variable-length keyword variable list.
kwargs is the variable name of the keyword variable list.
**kwargs is used to represent a variable-length of keyworded argument list.
**kwargs can also be used to pass a variable number of keyworded arguments to a function.
Python Packing and Unpacking
The interpretation mechanism of Python
*arg and
**kwarg syntaxes involves the packing and unpacking the variable-length variable list of a varable
Python Argument Packing and Unpacking
Python
*args is used to pack a variable-length of non-keyworded argument list into the variable
args. Python
*args can also be used to unpack the variable-length of non-keyworded argument list of variable
args into a variable-length of non-keyworded argument sequence.
*args Packing
Python
*args packing is used to create a new variable-length of non-keyworded argument list by packing a copy of assigned non-keyworded arguments into the variable
args accordingly.
*args Unpacking
Python
*args unpacking is used to create a new variable-length of non-keyworded argument sequence by unpacking a copy of the non-keyworded arguments of variable name
args accordingly.
Python Keyword Argument Packing and Unpacking
Python
**kwargs is used to pack a variable-length of keyworded argument list into the variable
kwargs. Python
**kwargs can also be used to unpack the variable-length of keyworded argument list of variable
kwargs into a variable-length of keyworded argument sequence.
**kwargs Packing
Python
*kwargs packing is used to create a new variable-length of keyworded argument list by packing a copy of assigned keyworded arguments into the variable
kwargs accordingly.
**kwargs Unpacking
Python
**kwargs unpacking is used to create a new variable-length of keyworded argument sequence by unpacking a copy of the keyworded arguments of variable name
kwargs accordingly.
Source and Reference