liveBook · Manning

Variadic arguments

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

A variadic positional parameter is a function parameter that collects an arbitrary number of positional arguments.

A variadic positional parameter lets a function accept an arbitrary number of positional arguments. The * marker identifies the parameter, whose value is collected as a tuple. Python’s print function uses this pattern. section 3.2.2

Collecting names

A function can require one name and collect the rest: def full_name(fname, *names): return " ".join([fname, *names]). This supports calls with multiple similar name arguments, but may hide the intended roles of those arguments. section 3.2.2

Use variadic arguments when the number of similar positional values should vary. Prefer explicitly named parameters when each value has a distinct meaning; explicit is better than implicit. section 3.2.2

def full_name(fname, *names):
    return " ".join([fname, *names])

See the progressive full_name example that collects additional positional arguments. 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 * syntax and tuple collection, then contrasts flexible argument lists with explicit named parameters.
Examples
Uses Python’s print function and a full_name function that joins one required name with collected additional names.
What it assumes
Assumes familiarity with function parameters and defaults, while explaining variable positional collection from its syntax onward.
What you can do after
Build it into functions that accept multiple similar positional values while recognizing when named parameters communicate intent better.
How it connects
Connects positional arguments with parameters, tuples, keyword parameters, and explicit API design.

Also explained in

Compare how 8 books explain variadic arguments →

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.