Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Several defaults, using names

  • non-keyword arg after keyword arg

Parameters with defaults must come at the end of the parameter declaration.

def f(a, b=2, c=3):
    print(a, b , c)

f(1)             # 1 2 3
f(1, b=0)        # 1 0 3
f(1, c=0)        # 1 2 0
f(1, c=0, b=5)   # 1 5 0

# f(b=0, 1)
# would generate:
# SyntaxError: non-keyword arg after keyword arg

f(b=0, a=1)      # 1 0 3


def f(a=2, b):
    print(a)
    print(b)

Output:

  File "examples/functions/named_and_positional_bad.py", line 2
    def f(a=2, b):
          ^
SyntaxError: non-default argument follows default argument

There can be several parameters with default values. They are all optional and can be given in any order after the positional arguments.