Back to course

Arbitrary Arguments: `**kwargs` (Keyword)

Python Programming: The 0 to Hero Bootcamp

Arbitrary Keyword Arguments (**kwargs)

**kwargs (keyword arguments) allows a function to accept an unknown number of arguments provided as key-value pairs.

Usage

When **kwargs is used, all extra keyword arguments passed to the function are collected into a dictionary named kwargs.

python def configure_settings(**options): # 'options' is a dictionary print("Configuration Received:")

for key, value in options.items():
    print(f"  {key}: {value}")

configure_settings(theme='light', log_level='INFO', cache_enabled=True, timeout=50)

Output:

Configuration Received: theme: light log_level: INFO cache_enabled: True timeout: 50

Full Parameter Order

When defining a function that uses all types of arguments, the order must be:

  1. Standard positional arguments.
  2. *args (arbitrary positional arguments).
  3. Default parameters.
  4. **kwargs (arbitrary keyword arguments).

python def function_signature(a, b, *args, d=10, **kwargs): pass