In Python, you can specify a variable number of arguments when calling a function by prefixing the parameter names with *
or **
in the function definition.
By convention, *args
(arguments) and **kwargs
(keyword arguments) are often used as parameter names, but you can use any name as long as it is prefixed with *
or **
. The sample code in this article uses *args
and **kwargs
.
Contents
- *args: Receive multiple arguments as a tuple
- **kwargs: Receive multiple keyword arguments as a dictionary
See the following article for the basics of functions in Python.
Additionally, using *
and **
when calling a function allows you to unpack and pass lists and dictionaries as arguments.
*args
: Receive multiple arguments as a tuple
By prefixing a parameter with *
, such as *args
, in the function definition, a function can accept any number of arguments.
def my_sum(*args): return sum(args)print(my_sum(1, 2, 3, 4))# 10print(my_sum(1, 2, 3, 4, 5, 6, 7, 8))# 36
source: args.py
Within the function, multiple arguments are received as a tuple. In this example, the tuple is then passed to the sum()
function 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
source: args.py
*args
can be used with positional arguments.
The values specified after the positional arguments are passed as a tuple to args
. If no extra positional arguments are passed, args
will be 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: ()
source: args.py
You can place *args
anywhere in the parameter list. However, any parameters defined after *args
must be specified using the keyword format parameter_name=value
when calling the function.
For example, in the following sample code, the last value passed to the function (in this case, 4
) must be specified as a keyword argument (arg2=4
). If it is not, a TypeError
will be raised.
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)
source: args.py
With this property, you can define a parameter named *
and treat any subsequent parameters as keyword-only arguments.
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 givenfunc_args_kw_only(100, arg2=200)# arg1: 100# arg2: 200
source: args.py
**kwargs
: Receive multiple keyword arguments as a dictionary
By prefixing a parameter with **
, such as **kwargs
, in the function definition, a function can accept any number of keyword arguments.
Within the function, multiple keyword arguments are received as a dictionary, with the argument names as keys and their values as the corresponding dictionary values.
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'>
source: kwargs.py
**kwargs
can also be used alongside positional arguments.
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}
source: kwargs.py
When calling a function, prefixing a dictionary with **
allows you to pass its elements as individual arguments.
d = {'key1': 1, 'key2': 2, 'arg1': 100, 'arg2': 200}func_kwargs_positional(**d)# arg1: 100# arg2: 200# kwargs: {'key1': 1, 'key2': 2}
source: kwargs.py
See the following article for details on unpacking function arguments.
A parameter prefixed with **
can only be defined at the end of the parameter list. If you define other parameters after the parameter, a SyntaxError
will be raised.
# def func_kwargs_error(**kwargs, arg):# print(kwargs)# SyntaxError: invalid syntax
source: kwargs.py