The following function arguments are probably the most common ones that stump you when you look at Python code and say, “What's this?
*args
**kwargs
Any number of arguments (variable-length arguments) can be specified by appending an asterisk to the argument in the function definition as follows
*
**
The names *args,**kwargs are often used as a convention. However, other names are acceptable as long as * and ** are at the beginning. The following sample code uses the names *args,**kwargs.
The following details are described below.
*args
:Accepts multiple arguments as a tuple**kwargs
:Accepts multiple keyword arguments as a dictionary
*args: Accepts multiple arguments as a tuple
Arbitrary number of arguments can be specified by defining arguments with *, as in *args.
def my_sum(*args):
return sum(args)
print(my_sum(1, 2, 3, 4))
# 10
print(my_sum(1, 2, 3, 4, 5, 6, 7, 8))
# 36
Multiple arguments are received as a tuple in the function. In the example, the sum() function is passed a tuple to calculate the sum.
def my_sum2(*args):
print('args: ', args)
print('type: ', type(args))
print('sum : ', sum(args))
my_sum2(1, 2, 3, 4)
# args: (1, 2, 3, 4)
# type: <class 'tuple'>
# sum : 10
It can also be combined with a position argument.
The value specified after (to the right of) the positional argument is passed to args as a tuple. If there is only a positional argument, it is an empty tuple.
def func_args(arg1, arg2, *args):
print('arg1: ', arg1)
print('arg2: ', arg2)
print('args: ', args)
func_args(0, 1, 2, 3, 4)
# arg1: 0
# arg2: 1
# args: (2, 3, 4)
func_args(0, 1)
# arg1: 0
# arg2: 1
# args: ()
Arguments marked with * may be defined first. In this case, however, arguments defined later than *args must be specified in keyword form. Incidentally, the keyword format is the “argument name = value” form.
The last value is not automatically passed to the positional argument. Therefore, if it is not specified as a keyword argument, a TypeError error will result.
def func_args2(arg1, *args, arg2):
print('arg1: ', arg1)
print('arg2: ', arg2)
print('args: ', args)
# func_args2(0, 1, 2, 3, 4)
# TypeError: func_args2() missing 1 required keyword-only argument: 'arg2'
func_args2(0, 1, 2, 3, arg2=4)
# arg1: 0
# arg2: 4
# args: (1, 2, 3)
If only * arguments are specified, subsequent arguments must always be specified as keyword arguments.(keyword-only argument
)
def func_args_kw_only(arg1, *, arg2):
print('arg1: ', arg1)
print('arg2: ', arg2)
# func_args_kw_only(100, 200)
# TypeError: func_args_kw_only() takes 1 positional argument but 2 were given
func_args_kw_only(100, arg2=200)
# arg1: 100
# arg2: 200
**kwargs: Accepts multiple keyword arguments as a dictionary
Arbitrary number of keyword arguments can be specified by defining arguments with ,** as in **kwargs.
In the function, the name of the argument is received as a dictionary whose key is the key and whose value is the value.
def func_kwargs(**kwargs):
print('kwargs: ', kwargs)
print('type: ', type(kwargs))
func_kwargs(key1=1, key2=2, key3=3)
# kwargs: {'key1': 1, 'key2': 2, 'key3': 3}
# type: <class 'dict'>
It can also be used in conjunction with a position argument.
def func_kwargs_positional(arg1, arg2, **kwargs):
print('arg1: ', arg1)
print('arg2: ', arg2)
print('kwargs: ', kwargs)
func_kwargs_positional(0, 1, key1=1)
# arg1: 0
# arg2: 1
# kwargs: {'key1': 1}
By specifying the dictionary object with ** as an argument when calling the function, it is possible to expand it and pass it as the respective argument.
d = {'key1': 1, 'key2': 2, 'arg1': 100, 'arg2': 200}
func_kwargs_positional(**d)
# arg1: 100
# arg2: 200
# kwargs: {'key1': 1, 'key2': 2}
Arguments marked with ** may only be defined at the end of the argument. Defining another argument after the argument marked with ** will result in a SyntaxError error.
# def func_kwargs_error(**kwargs, arg):
# print(kwargs)
# SyntaxError: invalid syntax