Repaso
Español
PythonCorePY-55

What does this print?

def f(*args, **kwargs):
    print(args, kwargs)

f(1, 2, mode="fast")

Options

  1. [1, 2] {'mode': 'fast'}
  2. (1, 2) {'mode': 'fast'} (Correct option)
  3. (1, 2, 'fast') {}
  4. 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.

Key terms

Practice this question in Repaso