note.nkmk.me

Variable-length arguments (*args, **kwargs) in Python

Posted: 2019-06-24 / Tags: Python

In Python, by adding * and ** (one or two asterisks) to the head of parameter names in the function definition, you can specify an arbitrary number of arguments (variable-length arguments) when calling the function.

By convention, the names *args (arguments) and **kwargs (keyword arguments) are often used, but as long as * and ** are headed, there are no problems with other names. The sample code below uses the names *args and **kwargs.

Here, the following contents will be described.

  • *args: Receive multiple arguments as a tuple
  • **kwargs: Receive multiple keyword arguments as a dictionary

See the following post for a basic explanation of functions in Python.

If you add * and ** when calling a function, not when defining a function, you can expand and pass lists and dictionaries to arguments. See the post below.

Sponsored Link

*args: Receive multiple arguments as a tuple

If you define a parameter preceded by * like *args, the function can receive any number of 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
source: args.py

In the function, multiple arguments are received as a tuple. In the example, a tuple is 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

It can also be combined with positional arguments.

The value specified after (right) of the positional argument is passed as a tuple to args. If only positional arguments, 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 define *args first, but in this case, parameters defined after *args must be specified by keyword format name=value at function call.

The last value is not automatically passed to the positional argument, and if it is not specified as a keyword argument, TypeError will occur.

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

Using this property, there is a technique of defining a parameer named * and using an parameter after that as a 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
source: args.py

**kwargs: Receive multiple keyword arguments as a dictionary

If you define a parameter preceded by ** like **kwargs, the function can receive any number of keyword arguments.

In the function, multiple keyword arguments are received as a dictionary whose key is argument name and whose value is 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'>
source: kwargs.py

It can also be combined with 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

By adding ** to a dictionary object when calling a function, you can pass each element to each argument.

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 post for details on argument expansion at function call.

A parameter with ** can only be defined at last of the parameter list. If you define another parameter after the parameter with **, SyntaxError will occur.

# def func_kwargs_error(**kwargs, arg):
#     print(kwargs)

# SyntaxError: invalid syntax
source: kwargs.py
Sponsored Link
Share

Related Categories

Related Posts