Repaso
Español
PythonIntermediatePY-22

What happens with f(1, 2)?

def f(a, *, b):
    return a + b

Options

  1. It returns 3, because both arguments are added together
  2. TypeError: the function takes 1 positional argument but 2 were given (Correct option)
  3. It runs, with b set to None because it was not named
  4. SyntaxError, because a bare * is not allowed in a definition

Answer

TypeError: takes 1 positional argument but 2 were given

Why

Every parameter after a lone * must be passed by name (keyword-only).

b is after the *, so f(1, 2) fails: the function accepts only one positional argument. The correct call is f(1, b=2).

This makes calls clearer. Parameters before a / are the opposite: positional-only.

Remember

After * → keyword-only. Before / → positional-only.

Key terms

Practice this question in Repaso