What does this print?
def f(*args, **kwargs):
print(args, kwargs)
f(1, 2, mode="fast")Options
- [1, 2] {'mode': 'fast'}
- (1, 2) {'mode': 'fast'} (Correct option)
- (1, 2, 'fast') {}
- An error
Answer
(1, 2) {'mode': 'fast'}
Why
*args collects extra positional arguments (passed without names) into a tuple: (1, 2).
**kwargs collects extra keyword arguments (passed as name=value) into a dict: {'mode': 'fast'}.
They let a function accept any number of arguments. Decorators use them to pass everything through.
Remember
*args → tuple of positional args. **kwargs → dict of named args.