mirror of https://github.com/python/cpython
#6570: clarify tutorial section about keyword arguments.
This commit is contained in:
parent
91621e2c16
commit
7b7e39a61f
|
@ -412,8 +412,8 @@ write the function like this instead::
|
||||||
Keyword Arguments
|
Keyword Arguments
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
Functions can also be called using keyword arguments of the form ``keyword =
|
Functions can also be called using :term:`keyword arguments <keyword argument>`
|
||||||
value``. For instance, the following function::
|
of the form ``kwarg=value``. For instance, the following function::
|
||||||
|
|
||||||
def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
|
def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
|
||||||
print("-- This parrot wouldn't", action, end=' ')
|
print("-- This parrot wouldn't", action, end=' ')
|
||||||
|
@ -421,26 +421,31 @@ value``. For instance, the following function::
|
||||||
print("-- Lovely plumage, the", type)
|
print("-- Lovely plumage, the", type)
|
||||||
print("-- It's", state, "!")
|
print("-- It's", state, "!")
|
||||||
|
|
||||||
could be called in any of the following ways::
|
accepts one required argument (``voltage``) and three optional arguments
|
||||||
|
(``state``, ``action``, and ``type``). This function can be called in any
|
||||||
|
of the following ways::
|
||||||
|
|
||||||
parrot(1000)
|
parrot(1000) # 1 positional argument
|
||||||
parrot(action = 'VOOOOOM', voltage = 1000000)
|
parrot(voltage=1000) # 1 keyword argument
|
||||||
parrot('a thousand', state = 'pushing up the daisies')
|
parrot(voltage=1000000, action='VOOOOOM') # 2 keyword arguments
|
||||||
parrot('a million', 'bereft of life', 'jump')
|
parrot(action='VOOOOOM', voltage=1000000) # 2 keyword arguments
|
||||||
|
parrot('a million', 'bereft of life', 'jump') # 3 positional arguments
|
||||||
|
parrot('a thousand', state='pushing up the daisies') # 1 positional, 1 keyword
|
||||||
|
|
||||||
but the following calls would all be invalid::
|
but all the following calls would be invalid::
|
||||||
|
|
||||||
parrot() # required argument missing
|
parrot() # required argument missing
|
||||||
parrot(voltage=5.0, 'dead') # non-keyword argument following keyword
|
parrot(voltage=5.0, 'dead') # non-keyword argument after a keyword argument
|
||||||
parrot(110, voltage=220) # duplicate value for argument
|
parrot(110, voltage=220) # duplicate value for the same argument
|
||||||
parrot(actor='John Cleese') # unknown keyword
|
parrot(actor='John Cleese') # unknown keyword argument
|
||||||
|
|
||||||
In general, an argument list must have any positional arguments followed by any
|
In a function call, keyword arguments must follow positional arguments.
|
||||||
keyword arguments, where the keywords must be chosen from the formal parameter
|
All the keyword arguments passed must match one of the arguments
|
||||||
names. It's not important whether a formal parameter has a default value or
|
accepted by the function (e.g. ``actor`` is not a valid argument for the
|
||||||
not. No argument may receive a value more than once --- formal parameter names
|
``parrot`` function), and their order is not important. This also includes
|
||||||
corresponding to positional arguments cannot be used as keywords in the same
|
non-optional arguments (e.g. ``parrot(voltage=1000)`` is valid too).
|
||||||
calls. Here's an example that fails due to this restriction::
|
No argument may receive a value more than once.
|
||||||
|
Here's an example that fails due to this restriction::
|
||||||
|
|
||||||
>>> def function(a):
|
>>> def function(a):
|
||||||
... pass
|
... pass
|
||||||
|
|
Loading…
Reference in New Issue