Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b7e39a

Browse files
committed
#6570: clarify tutorial section about keyword arguments.
1 parent 91621e2 commit 7b7e39a

1 file changed

Lines changed: 23 additions & 18 deletions

File tree

Doc/tutorial/controlflow.rst

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -412,35 +412,40 @@ write the function like this instead::
412412
Keyword Arguments
413413
-----------------
414414

415-
Functions can also be called using keyword arguments of the form ``keyword =
416-
value``. For instance, the following function::
415+
Functions can also be called using :term:`keyword arguments <keyword argument>`
416+
of the form ``kwarg=value``. For instance, the following function::
417417

418418
def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
419419
print("-- This parrot wouldn't", action, end=' ')
420420
print("if you put", voltage, "volts through it.")
421421
print("-- Lovely plumage, the", type)
422422
print("-- It's", state, "!")
423423

424-
could be called in any of the following ways::
424+
accepts one required argument (``voltage``) and three optional arguments
425+
(``state``, ``action``, and ``type``). This function can be called in any
426+
of the following ways::
425427

426-
parrot(1000)
427-
parrot(action = 'VOOOOOM', voltage = 1000000)
428-
parrot('a thousand', state = 'pushing up the daisies')
429-
parrot('a million', 'bereft of life', 'jump')
428+
parrot(1000) # 1 positional argument
429+
parrot(voltage=1000) # 1 keyword argument
430+
parrot(voltage=1000000, action='VOOOOOM') # 2 keyword arguments
431+
parrot(action='VOOOOOM', voltage=1000000) # 2 keyword arguments
432+
parrot('a million', 'bereft of life', 'jump') # 3 positional arguments
433+
parrot('a thousand', state='pushing up the daisies') # 1 positional, 1 keyword
430434

431-
but the following calls would all be invalid::
435+
but all the following calls would be invalid::
432436

433437
parrot() # required argument missing
434-
parrot(voltage=5.0, 'dead') # non-keyword argument following keyword
435-
parrot(110, voltage=220) # duplicate value for argument
436-
parrot(actor='John Cleese') # unknown keyword
437-
438-
In general, an argument list must have any positional arguments followed by any
439-
keyword arguments, where the keywords must be chosen from the formal parameter
440-
names. It's not important whether a formal parameter has a default value or
441-
not. No argument may receive a value more than once --- formal parameter names
442-
corresponding to positional arguments cannot be used as keywords in the same
443-
calls. Here's an example that fails due to this restriction::
438+
parrot(voltage=5.0, 'dead') # non-keyword argument after a keyword argument
439+
parrot(110, voltage=220) # duplicate value for the same argument
440+
parrot(actor='John Cleese') # unknown keyword argument
441+
442+
In a function call, keyword arguments must follow positional arguments.
443+
All the keyword arguments passed must match one of the arguments
444+
accepted by the function (e.g. ``actor`` is not a valid argument for the
445+
``parrot`` function), and their order is not important. This also includes
446+
non-optional arguments (e.g. ``parrot(voltage=1000)`` is valid too).
447+
No argument may receive a value more than once.
448+
Here's an example that fails due to this restriction::
444449

445450
>>> def function(a):
446451
... pass

0 commit comments

Comments
 (0)