liveBook · Manning

**kwargs

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

A variadic keyword parameter is a function parameter that collects an arbitrary number of keyword arguments into a mapping.

When a function needs to accept flexible named options, a keyword parameter dictionary collects them into one mapping. **kwargs is the conventional name, not a required one. The function can inspect expected keys, while extra pairs may be ignored or passed to another function. Use this flexibility intentionally: explicit, named parameters are often clearer when the accepted data is known. section 3.2.2

Inspecting named values

A function such as full_name(**kwargs) can look up fname, mname, and lname in kwargs. This supports keyword-based calls without fixing the parameter list, but users need documentation to know which keys matter. Unrecognized key-value pairs have no effect unless the function forwards the mapping to another function. section 3.2.2

def full_name(**kwargs):
    [CA]return f"{kwargs.get('fname', '')} {kwargs.get('mname', '')}
{kwargs('lname', '')}"

Inspect the compact full_name definition to see **kwargs used as a dictionary-like collection of named values. section 3.2.2

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Defines the collection and lookup behavior, then cautions that undocumented keys and intentional forwarding require care.
Examples
Uses full_name to inspect fname, mname, and lname while contrasting flexible collection with explicitly named parameters.
What it assumes
Assumes familiarity with functions and parameters, while clarifying how keyword values become one dictionary-like parameter.
What you can do after
Apply it via Python function definitions to collect, inspect, ignore, or forward arbitrary keyword arguments deliberately.
How it connects
Connects keyword arguments with dictionaries, function parameters, function signatures, and API consistency.

Also explained in

Compare how 3 books explain **kwargs →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.