**kwargs

Extra named arguments are collected into a dictionary. Read them with .get(key, default).

Syntax

def greet(name, **opts):
return opts.get("title", "bot") + " " + name
  • **opts only collects named values, not positional ones.

Example

def greet(name, **opts):
    return opts.get("title", "bot") + " " + name

print(greet("Pip"))
print(greet("Pip", title="captain"))

Output:

bot Pip
captain Pip

Common mistakes

  • Passing a second positional argument into a function that only has **opts for extras is a TypeError.

Practise it