Featureintro
Keyword arguments
Also called keyword argument, named argument, kwarg=value, kwargs, double star parameter, call by name, got multiple values for argument.
A call can give an argument as name=value. Keyword arguments come after positional arguments, they can be in any order, and each one must match a parameter name or go to a **kwargs parameter.
- Language
- Python
Synopsis
def parrot(voltage, state="a stiff", action="voom"):
return f"{action} at {voltage} volts, {state}"
print(parrot(1000))
print(parrot(voltage=1000))
print(parrot(action="VOOOOOM", voltage=1000000))
print(parrot("a thousand", state="pushing up the daisies"))
voom at 1000 volts, a stiff
voom at 1000 volts, a stiff
VOOOOOM at 1000000 volts, a stiff
voom at a thousand volts, pushing up the daisies
A keyword argument names its parameter, so the order of keyword arguments is not important.
Semantics
r1. "Functions can also be called using keyword arguments of the form kwarg=value."1
r2. "In a function call, keyword arguments must follow positional arguments. All the keyword arguments passed must match one of the arguments accepted by the function (e.g. actor is not a valid argument for the parrot function), and their order is not important."1 Examples: positional-after-keyword, unknown-keyword.
r3. "No argument may receive a value more than once."1 Example: twice.
r4. "When a final formal parameter of the form **name is present, it receives a dictionary" that contains "all keyword arguments except for those corresponding to a formal parameter."1 Example: kwargs.
r5. "Note that the order in which the keyword arguments are printed is guaranteed to match the order in which they were provided in the function call."1 Example: kwargs.
r6. For each keyword argument, "the identifier is used to determine the corresponding slot".2 "If the slot is already filled, a TypeError exception is raised."2
Examples
def tag(name, **attributes):
parts = [name] + [f'{k}="{v}"' for k, v in attributes.items()]
return "<" + " ".join(parts) + ">"
print(tag("a", href="/home", title="Home"))
print(tag("br"))
<a href="/home" title="Home">
<br>
attributes is a dict of the keyword arguments that match no parameter. The keys keep the order of the call (rules r4 and r5).
Pitfalls
A value for the same parameter by position and by keyword is a TypeError:
This code raises TypeError, on purpose.
def function(a):
pass
function(0, a=0)
A name that matches no parameter is a TypeError when there is no ** parameter:
This code raises TypeError, on purpose.
def parrot(voltage, state="a stiff"):
pass
parrot(actor="John Cleese")
A positional argument after a keyword argument is a syntax error:
This code raises SyntaxError, on purpose.
def parrot(voltage, state="a stiff"):
pass
parrot(voltage=5.0, "dead")
Comparison
Go has no keyword arguments. In a Go call f(a1, a2, … an), Go assigns the arguments to the parameters in order: "the arguments of the call are passed to the function, which means that they are assigned to their corresponding function parameters".3 In Python, a keyword argument fills the parameter with the same name (rule r6).
References
- The Python Tutorial, 4. More Control Flow Tools, 4.9.2. Keyword Arguments
- The Python Language Reference, Python 3.15, 6.3.3. Calls
- The Go Programming Language Specification, Calls
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag CG0JCG Edit this page History Report an error Markdown