Back
+10 XP
Concept

Arbitrary Keyword Arguments: `**kwargs`

Prefix a parameter with `**kwargs` (double asterisks) to slurp any number of keyword arguments into a clean dictionary.

python
def build_profile(**data):
    print("Received dictionary:", data)
    return data.get("role", "guest")

print(build_profile(name="Sam", role="admin"))

# 95. Arbitrary Keyword Arguments: `**kwargs` Prefix a parameter with `**kwargs` (double asterisks) to slurp any number of keyword arguments into a clean dictionary: ```python def build_profile(**data): print("Received dictionary:", data) return data.get("role", "guest") print(build_profile(name="Sam", role="admin")) ``` Combining both: `def universal(*args, **kwargs):` can intercept literally any function call in the universe, positional or keyword. This is the foundation of Python decorators and middleware.

Checkpoint

What data type is `kwargs` inside a function defined with `**kwargs`?

Practice

Write a function `dump_keys(**kwargs)` that returns a list of all passed key names using `list(kwargs.keys())`. Call `dump_keys(a=1, b=2)` and print the result.

Terminal
01
Tap code blocks below to assemble
Available Blocks