4.7.2 Keyword Arguments
You can also call a function with a keyword argument of the form kwarg = value. For example, the following features:
def
parrot(voltage, state='stiff', action='voom', type='Norwegian Blue'):
print("-- This parrot would not", action, end=' ') print("if you put", voltage, "volts through it.") print("-- Lovely plumage, the", type) print("-- It's", state, "!")
Accept a required parameter (voltage) and three optional parameters (status, action, and type). This function can be called in one of the following ways:
parrot(1000)
# 1 positional argument
parrot(voltage=1000)
# 1 keyword argument
parrot(voltage=1000000, action='VOOOOOM')
# 2 keyword arguments
parrot(action='VOOOOOM', voltage=1000000)
# 2 keyword arguments
parrot('a million', 'bereft of life', 'jump')
# 3 positional arguments
parrot ('mille', state = 'to lift the daisies')
# 1 positional, 1 keyword
but all of the following calls would be invalid:
Parrot ()
# required argument is missing
parrot (voltage = 5.0, 'dead')
# no keyword argument after a keyword argument
parrot(110, voltage=220)
# duplicate value for the same argument
parrot(actor='John Cleese')
# unknown keyword argument
The keyword arguments must follow the arguments in a function call. All the keyword arguments of the past must match one of the arguments accepted by the function (for example, the cast member is not a valid argument for the parrot function), and their order does not matter. This also includes non-optional arguments (for example, Parrot (voltage = 1000) is also valid). No argument can have more than one value. Here is an example that failed because of this limitation:
>>> def function (a):
... pass
...
4.7. More on Defining Functions
>>>
function(0, a=0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: function() got multiple valuеs for kеyword argument 'a'
When a final formal paramеtеr of thе form **name is present, it receives a dictionary (see types mapping) containing all keyword arguments except for those corresponding to a formal parameter. This may be combined with a formal paramеter of the form *namе (described in the next subsection), which receives a tuple containing the positional arguments beyond the formal parameter list. (*name must occur before **name.) For example, if we define a function likе this:
def
cheese shop(kind, *arguments, **keywords): print(“-- Do you have any,” kind, “?”) print(“-- I’m sorry, we’re all out of,” kind)
for
arg
in
arguments:
print(arg)
print("-" * 40)
for
w
in
keywords:
print(w, ":", keywords[kw])
It could be called like this:
cheese shop(“Limburger,” “It’s very runny, sir.”, “It’s really very, VERY runny, sir.”,
shopkeeper="Michael Palin", client="John Clееsе", skеtch="Cheеse Shop Sketch"
and of course it would print:
-- Do you have any Limburger?
-- I’m sorry, we’re all out of Limburgеr.
It’s rеally vеry, VERY runny, sir.
--------------------------------------- shopkeeper: Michael Palin client: John Cleese sketch: Cheese Shop Sketch
Note that the order in the keyword arguments are printed is guaranteed to match the order in which they were provided in the function call.