Defining a function capable of taking an arbitrary number of arguments can be done by prefixing one of the arguments with a \\*
def func(*args):
# args will be a tuple containing all values that are passed in
for i in args:
print(i)
func(1, 2, 3) # Calling it with 3 arguments
# Out: 1
# 2
# 3
list_of_arg_values = [1, 2, 3]
func(*list_of_arg_values) # Calling it with list of values, * expands the list
# Out: 1
# 2
# 3
func() # Calling it without arguments
# No Output
You can’t provide a default for args
, for example func(*args=[1, 2, 3])
will raise a syntax error (won’t even compile).
You can’t provide these by name when calling the function, for example func(*args=[1, 2, 3])
will raise a TypeError
.
But if you already have your arguments in an array (or any other Iterable
), you can invoke your function like this: func(*my_stuff)
.
These arguments (*args
) can be accessed by index, for example args[0]
will return the first argument
You can take an arbitrary number of arguments with a name by defining an argument in the definition with two \\*
in front of it:
def func(**kwargs):
# kwargs will be a dictionary containing the names as keys and the values as values
for name, value in kwargs.items():
print(name, value)
func(value1=1, value2=2, value3=3) # Calling it with 3 arguments
# Out: value1 1
# value2 2
# value3 3
func() # Calling it without arguments
# No Out put
my_dict = {'foo': 1, 'bar': 2}
func(**my_dict) # Calling it with a dictionary
# Out: foo 1
# bar 2
You can’t provide these without names, for example func(1, 2, 3)
will raise a TypeError
.
kwargs
is a plain native python dictionary. For example, args['value1']
will give the value for argument value1
. Be sure to check beforehand that there is such an argument or a KeyError
will be raised.
You can mix these with other optional and required arguments but the order inside the definition matters.
The positional/keyword arguments come first. (Required arguments).
Then comes the arbitrary *arg
arguments. (Optional).
Then keyword-only arguments come next. (Required).
Finally the arbitrary keyword **kwargs
come. (Optional).
# |-positional-|-optional-|---keyword-only--|-optional-|
def func(arg1, arg2=10 , *args, kwarg1, kwarg2=2, **kwargs):
pass
arg1
must be given, otherwise a TypeError
is raised. It can be given as positional (func(10)
) or keyword argument (func(arg1=10)
).