note.nkmk.me

Default arguments in Python

Posted: 2019-06-24 / Tags: Python

In Python, you can set default values for arguments when defining functions. If you omit the argument when calling the function, the default value will be used.

Here, the following contents will be described.。

  • Setting default argument values
  • Position constraints of default arguments
  • Notes on using lists and dictionaries as default values

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

Sponsored Link

Setting default argument values

If you use argument_name=default_value in the function definition, the default value will be used when the corresponding argument is omitted.

def func_default(arg1, arg2='default_x', arg3='default_y'):
    print(arg1)
    print(arg2)
    print(arg3)

func_default('a')
# a
# default_x
# default_y

func_default('a', 'b')
# a
# b
# default_y

func_default('a', arg3='c')
# a
# default_x
# c

Position constraints of default arguments

Placing a default argument before an ordinary argument (arguments without a default value) at function definition causes a SyntaxError.

# def func_default_error(arg2='default_a', arg3='default_b', arg1):
#     print(arg1)
#     print(arg2)

# SyntaxError: non-default argument follows default argument
Sponsored Link

Notes on using lists and dictionaries as default values

If a mutable object such as a list or a dictionary is specified as a default value, the object is created at function definition, and the same object is used when the function is called with the corresponding argument omitted.

Default parameter values are evaluated from left to right when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that the same “pre-computed” value is used for each call.
8. Compound statements - Function definitions — Python 3.7.4rc1 documentation

Therefore, for example, if you define a function that adds an element to a list or dictionary as a default argument and omit the argument and call it multiple times, the element will be added repeatedly to the same object.

Example for a list:

def func_default_list(l=[0, 1, 2], v=3):
    l.append(v)
    print(l)

func_default_list([0, 0, 0], 100)
# [0, 0, 0, 100]

func_default_list()
# [0, 1, 2, 3]

func_default_list()
# [0, 1, 2, 3, 3]

func_default_list()
# [0, 1, 2, 3, 3, 3]

Example for a dictionary:

def func_default_dict(d={'default': 0}, k='new', v=100):
    d[k] = v
    print(d)

func_default_dict()
# {'default': 0, 'new': 100}

func_default_dict(k='new2', v=200)
# {'default': 0, 'new': 100, 'new2': 200}

Use None to create a new object if the argument is omitted. A new object is created each time the function is called.

def func_default_list_none(l=None, v=3):
    if l is None:
        l = [0, 1, 2]
    l.append(v)
    print(l)

func_default_list_none()
# [0, 1, 2, 3]

func_default_list_none()
# [0, 1, 2, 3]
Sponsored Link
Share

Related Categories

Related Posts