What happens with f(1, 2)?
def f(a, *, b):
return a + bOptions
- It returns 3, because both arguments are added together
- TypeError: the function takes 1 positional argument but 2 were given (Correct option)
- It runs, with
bset to None because it was not named - 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.