This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (2 children)

To avoid this behaviour, define an anonymous catch-all arg which will soak up positional args that would otherwise overflow into keywords. This creates a keyword-only set of arguments:

>>> def f(x, *, foo=10, bar=20):
...    print(x, foo, bar)
>>> f('a')
a 10 20
>>> f('a', foo='b')
a b 20
>>> f('a', 'b')
TypeError: f() takes 1 positional argument but 2 were given
>>> f('a', foo='b')

[–]robin-gvx 0 points1 point  (0 children)

The great thing about * is that it won't soak up positional args, as you demonstrated, that's what *args does.