liveBook · Manning

Named argument

As explained in Python How-To

A keyword argument is a function argument supplied by associating an explicit parameter name with its value.

A named argument, also called a keyword argument, supplies a value by naming the corresponding parameter. Unlike positional arguments, keyword arguments do not depend on their order. section 6.4.1

Call a function with named arguments

For a function such as `def multiply_numbers(a, b): return a * b`, these calls produce the same result: `multiply_numbers(2, 3)` and `multiply_numbers(a=2, b=3)`. Reordering the names remains valid: `multiply_numbers(b=3, a=2)`. When combining styles, place positional arguments first: `multiply_numbers(2, b=3)`. section 6.4.1

Use named arguments to make function calls clearer and to handle flexible interfaces:

  • Match values to parameters by identifier rather than by position. section 6.4.1
  • Keep positional arguments before named arguments; otherwise Python raises a `SyntaxError`. section 6.4.1
  • Accept an arbitrary number of named arguments with `**kwargs`; Python collects them into a dictionary. section 6.4.3
  • Use valid identifiers for keyword names; names cannot begin with a number. section 6.4.5
Figure 6.6 Using positional and keyword arguments in function calls. If the arguments have identifiers preceding the arguments, they’re keyword arguments. If the arguments have no identifiers, they’re positional arguments.
CH06_F06_Cui

Compare positional and named arguments visually through representative function calls. section 6.4.1

How Python How-To covers it

How deep it goes
Explains identifier-based matching, ordering rules, flexible **kwargs, and invalid names without descending into interpreter internals.
Examples
Demonstrates multiply_numbers calls, print's named options, create_report with grades, and valid or invalid **kwargs calls.
What it assumes
Assumes basic familiarity with Python functions, parameters, and calls while distinguishing positional and named argument behavior explicitly.
What you can do after
Apply named arguments in function calls, combine them correctly with positional arguments, and define flexible interfaces with **kwargs.
How it connects
Connects named arguments with positional arguments, **kwargs, default arguments, type hints, and docstrings.

Also explained in

Compare how 3 books explain named argument →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.