Skip to content

Commit 6685c45

Browse files
committed
Update function related explanations on "controlflow.rst"
1 parent 149c465 commit 6685c45

File tree

1 file changed

+61
-59
lines changed

1 file changed

+61
-59
lines changed

Doc/tutorial/controlflow.rst

Lines changed: 61 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ boundary::
488488
single: strings, documentation
489489

490490
The keyword :keyword:`def` introduces a function *definition*. It must be
491-
followed by the function name and the parenthesized list of formal parameters.
491+
followed by the function name and the parenthesized list of parameters.
492492
The statements that form the body of the function start at the next line, and
493493
must be indented.
494494

@@ -510,7 +510,7 @@ variables, named in a :keyword:`global` statement, or, for variables of enclosin
510510
functions, named in a :keyword:`nonlocal` statement), although they may be
511511
referenced.
512512

513-
The actual parameters (arguments) to a function call are introduced in the local
513+
Arguments to a function call are introduced in the local
514514
symbol table of the called function when it is called; thus, arguments are
515515
passed using *call by value* (where the *value* is always an object *reference*,
516516
not the value of the object). [#]_ When a function calls another function,
@@ -578,16 +578,16 @@ This example, as usual, demonstrates some new Python features:
578578
More on Defining Functions
579579
==========================
580580

581-
It is also possible to define functions with a variable number of arguments.
581+
It is also possible to define functions with variadic arguments.
582582
There are three forms, which can be combined.
583583

584584

585-
.. _tut-defaultargs:
585+
.. _tut-defaultparams:
586586

587-
Default Argument Values
587+
Default Parameter Values
588588
-----------------------
589589

590-
The most useful form is to specify a default value for one or more arguments.
590+
The most useful form is to specify a default value for one or more parameters.
591591
This creates a function that can be called with fewer arguments than it is
592592
defined to allow. For example::
593593

@@ -690,11 +690,11 @@ but all the following calls would be invalid::
690690
parrot(actor='John Cleese') # unknown keyword argument
691691

692692
In a function call, keyword arguments must follow positional arguments.
693-
All the keyword arguments passed must match one of the arguments
694-
accepted by the function (e.g. ``actor`` is not a valid argument for the
695-
``parrot`` function), and their order is not important. This also includes
693+
Each keyword arguments passed must match one of the function parameters
694+
(e.g. ``actor`` is not a valid argument for the ``parrot`` function),
695+
and their order is not important. This also includes
696696
non-optional arguments (e.g. ``parrot(voltage=1000)`` is valid too).
697-
No argument may receive a value more than once.
697+
No arguments are possible only if no required parameters exist.
698698
Here's an example that fails due to this restriction::
699699

700700
>>> def function(a):
@@ -703,15 +703,15 @@ Here's an example that fails due to this restriction::
703703
>>> function(0, a=0)
704704
Traceback (most recent call last):
705705
File "<stdin>", line 1, in <module>
706-
TypeError: function() got multiple values for argument 'a'
706+
TypeError: function() got multiple values for parameter 'a'
707707

708-
When a final formal parameter of the form ``**name`` is present, it receives a
709-
dictionary (see :ref:`typesmapping`) containing all keyword arguments except for
710-
those corresponding to a formal parameter. This may be combined with a formal
711-
parameter of the form ``*name`` (described in the next subsection) which
712-
receives a :ref:`tuple <tut-tuples>` containing the positional
713-
arguments beyond the formal parameter list. (``*name`` must occur
714-
before ``**name``.) For example, if we define a function like this::
708+
When a final var-keyword parameter ``**name`` is present, it defaults to an empty
709+
dictionary (see :ref:`typesmapping`) to receive all keyword arguments except for
710+
those corresponding to other parameters. This may be combined with a var-positional
711+
parameter ``*name`` (described in the next subsection) which defaults to an empty
712+
:ref:`tuple <tut-tuples>` to receive all positional arguments except for those received
713+
by the preceding parameters. (``*name`` must occur before ``**name``.) For example,
714+
if we define a function like this::
715715

716716
def cheeseshop(kind, *arguments, **keywords):
717717
print("-- Do you have any", kind, "?")
@@ -749,7 +749,7 @@ to match the order in which they were provided in the function call.
749749
Special parameters
750750
------------------
751751

752-
By default, arguments may be passed to a Python function either by position
752+
By default, arguments can be passed to a Python function either by position
753753
or explicitly by keyword. For readability and performance, it makes sense to
754754
restrict the way arguments can be passed so that a developer need only look
755755
at the function definition to determine if items are passed by position, by
@@ -772,33 +772,34 @@ positional-only, positional-or-keyword, and keyword-only. Keyword parameters
772772
are also referred to as named parameters.
773773

774774
-------------------------------
775-
Positional-or-Keyword Arguments
775+
Positional-or-Keyword Parameters
776776
-------------------------------
777777

778-
If ``/`` and ``*`` are not present in the function definition, arguments may
779-
be passed to a function by position or by keyword.
778+
Positional-or-keyword paremeters don't precede ``/``, don't follow ``*`` or ``*args``,
779+
and don't have ``*`` and ``**`` in their names as prefixes, so that arguments can
780+
be passed to them by position or keyword.
780781

781782
--------------------------
782783
Positional-Only Parameters
783784
--------------------------
784785

785786
Looking at this in a bit more detail, it is possible to mark certain parameters
786-
as *positional-only*. If *positional-only*, the parameters' order matters, and
787-
the parameters cannot be passed by keyword. Positional-only parameters are
787+
as *positional-only*. If *positional-only*, the arguments' order matters, and
788+
the arguments cannot be passed by keyword. Positional-only parameters are
788789
placed before a ``/`` (forward-slash). The ``/`` is used to logically
789790
separate the positional-only parameters from the rest of the parameters.
790791
If there is no ``/`` in the function definition, there are no positional-only
791792
parameters.
792793

793-
Parameters following the ``/`` may be *positional-or-keyword* or *keyword-only*.
794+
Parameters following the ``/`` can be *positional-or-keyword* or *keyword-only*.
794795

795796
----------------------
796-
Keyword-Only Arguments
797+
Keyword-Only Parameters
797798
----------------------
798799

799-
To mark parameters as *keyword-only*, indicating the parameters must be passed
800-
by keyword argument, place an ``*`` in the arguments list just before the first
801-
*keyword-only* parameter.
800+
To mark parameters as *keyword-only*, indicating their arguments must be passed
801+
by keyword, place parameters after an ``*`` to make them
802+
*keyword-only* parameters.
802803

803804
-----------------
804805
Function Examples
@@ -807,49 +808,49 @@ Function Examples
807808
Consider the following example function definitions paying close attention to the
808809
markers ``/`` and ``*``::
809810

810-
>>> def standard_arg(arg):
811-
... print(arg)
811+
>>> def standard_param(x):
812+
... print(x)
812813
...
813-
>>> def pos_only_arg(arg, /):
814-
... print(arg)
814+
>>> def pos_only_param(x, /):
815+
... print(x)
815816
...
816-
>>> def kwd_only_arg(*, arg):
817-
... print(arg)
817+
>>> def kwd_only_param(*, x):
818+
... print(x)
818819
...
819820
>>> def combined_example(pos_only, /, standard, *, kwd_only):
820821
... print(pos_only, standard, kwd_only)
821822

822823

823-
The first function definition, ``standard_arg``, the most familiar form,
824-
places no restrictions on the calling convention and arguments may be
824+
The first function definition, ``standard_param``, the most familiar form,
825+
places no restrictions on the calling convention and arguments can be
825826
passed by position or keyword::
826827

827-
>>> standard_arg(2)
828+
>>> standard_param(2)
828829
2
829830

830-
>>> standard_arg(arg=2)
831+
>>> standard_param(x=2)
831832
2
832833

833-
The second function ``pos_only_arg`` is restricted to only use positional
834-
parameters as there is a ``/`` in the function definition::
834+
The second function ``pos_only_param`` is restricted to only use a positional
835+
argument as its parameter precedes ``/``::
835836

836-
>>> pos_only_arg(1)
837+
>>> pos_only_param(1)
837838
1
838839

839-
>>> pos_only_arg(arg=1)
840+
>>> pos_only_param(x=1)
840841
Traceback (most recent call last):
841842
File "<stdin>", line 1, in <module>
842-
TypeError: pos_only_arg() got some positional-only arguments passed as keyword arguments: 'arg'
843+
TypeError: pos_only_param() got some positional arguments passed as keyword arguments: 'x'
843844

844-
The third function ``kwd_only_arg`` only allows keyword arguments as indicated
845+
The third function ``kwd_only_param`` only allows keyword arguments as indicated
845846
by a ``*`` in the function definition::
846847

847-
>>> kwd_only_arg(3)
848+
>>> kwd_only_param(3)
848849
Traceback (most recent call last):
849850
File "<stdin>", line 1, in <module>
850-
TypeError: kwd_only_arg() takes 0 positional arguments but 1 was given
851+
TypeError: kwd_only_param() takes 0 positional arguments but 1 was given
851852

852-
>>> kwd_only_arg(arg=3)
853+
>>> kwd_only_param(x=3)
853854
3
854855

855856
And the last uses all three calling conventions in the same function
@@ -869,34 +870,35 @@ definition::
869870
>>> combined_example(pos_only=1, standard=2, kwd_only=3)
870871
Traceback (most recent call last):
871872
File "<stdin>", line 1, in <module>
872-
TypeError: combined_example() got some positional-only arguments passed as keyword arguments: 'pos_only'
873+
TypeError: combined_example() got some positional arguments passed as keyword arguments: 'pos_only'
873874

874875

875-
Finally, consider this function definition which has a potential collision between the positional argument ``name`` and ``**kwds`` which has ``name`` as a key::
876+
Finally, consider this function which has a potential collision between
877+
the positional-or-keyword parameter ``name`` and the var-keyword parameter
878+
``**kwds`` which has ``name`` as a key::
876879

877880
def foo(name, **kwds):
878881
return 'name' in kwds
879882

880-
There is no possible call that will make it return ``True`` as the keyword ``'name'``
881-
will always bind to the first parameter. For example::
883+
Calling `foo()` gets error because `name` parameter can receive both the 1st and
884+
2nd argument by position and keyword respectively::
882885

883-
>>> foo(1, **{'name': 2})
886+
>>> foo(1, name=2)
884887
Traceback (most recent call last):
885888
File "<stdin>", line 1, in <module>
886-
TypeError: foo() got multiple values for argument 'name'
889+
TypeError: foo() got multiple values for parameter 'name'
887890
>>>
888891

889-
But using ``/`` (positional only arguments), it is possible since it allows ``name`` as a positional argument and ``'name'`` as a key in the keyword arguments::
892+
But using ``/`` (positional-only parameters) and calling `foo()` works because
893+
``name`` parameter can only receive the 1st argument by position while
894+
`**kwds` receives the 2nd argument by keyword::
890895

891896
>>> def foo(name, /, **kwds):
892897
... return 'name' in kwds
893898
...
894-
>>> foo(1, **{'name': 2})
899+
>>> foo(1, name=2)
895900
True
896901

897-
In other words, the names of positional-only parameters can be used in
898-
``**kwds`` without ambiguity.
899-
900902
-----
901903
Recap
902904
-----

0 commit comments

Comments
 (0)