*args and **kwargs in Python (Variable-Length Arguments)
In Python, you can define functions that accept a variable number of arguments by prefixing parameter names with *
or **
in the function definition.
By convention, *args
(arguments) and **kwargs
(keyword arguments) are commonly 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
.
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
Receives Multiple Positional Arguments as a Tuple
When you prefix a parameter with *
in the function definition, the function can accept any number of positional arguments.
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
Inside the function, these arguments are received as a tuple. In this example, we pass the tuple to the sum()
function to calculate the total.
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
You can combine *args
with regular positional arguments.
Any additional positional arguments are collected into a tuple. If no additional arguments are provided, 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: ()
You can place *args
anywhere in the parameter list. However, any parameters defined after *args
must be specified using keyword arguments (parameter_name=value
).
For example, in the following code, the last value 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)
You can also use a bare *
to force all following parameters to be 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 given
func_args_kw_only(100, arg2=200)
# arg1: 100
# arg2: 200
**kwargs
Receives Multiple Keyword Arguments as a Dictionary
When you prefix a parameter with **
in the function definition, the function can accept any number of keyword arguments.
Inside the function, these arguments are received as a dictionary, where the argument names become the keys and their values become 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'>
You can also use **kwargs
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}
When calling a function, you can use **
to unpack a dictionary and pass its key-value pairs as keyword arguments.
d = {'key1': 1, 'key2': 2, 'arg1': 100, 'arg2': 200}
func_kwargs_positional(**d)
# arg1: 100
# arg2: 200
# kwargs: {'key1': 1, 'key2': 2}
See the following article for details on unpacking function arguments.
A parameter prefixed with **
must be the last parameter in the function definition. If you try to define other parameters after it, a SyntaxError
will be raised.
# def func_kwargs_error(**kwargs, arg):
# print(kwargs)
# SyntaxError: arguments cannot follow var-keyword argument