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

Skip to content

Commit c2a7fd6

Browse files
committed
Improve argument/parameter documentation (issue #15990).
This commit adds "parameter" to the glossary, improves and consolidates the "argument" glossary entry, and adds a question to the FAQ on the difference between arguments and parameters.
1 parent 14b04cd commit c2a7fd6

3 files changed

Lines changed: 99 additions & 18 deletions

File tree

Doc/faq/programming.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,27 @@ calling another function by using ``*`` and ``**``::
313313
g(x, *args, **kwargs)
314314

315315

316+
.. _faq-argument-vs-parameter:
317+
318+
What is the difference between arguments and parameters?
319+
--------------------------------------------------------
320+
321+
:term:`Parameters <parameter>` are defined by the names that appear in a
322+
function definition, whereas :term:`arguments <argument>` are the values
323+
actually passed to a function when calling it. Parameters define what types of
324+
arguments a function can accept. For example, given the function definition::
325+
326+
def func(foo, bar=None, **kwargs):
327+
pass
328+
329+
*foo*, *bar* and *kwargs* are parameters of ``func``. However, when calling
330+
``func``, for example::
331+
332+
func(42, bar=314, extra=somevar)
333+
334+
the values ``42``, ``314``, and ``somevar`` are arguments.
335+
336+
316337
How do I write a function with output parameters (call by reference)?
317338
---------------------------------------------------------------------
318339

Doc/glossary.rst

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,34 @@ Glossary
4040
ABCs with the :mod:`abc` module.
4141

4242
argument
43-
A value passed to a function or method, assigned to a named local
44-
variable in the function body. A function or method may have both
45-
positional arguments and keyword arguments in its definition.
46-
Positional and keyword arguments may be variable-length: ``*`` accepts
47-
or passes (if in the function definition or call) several positional
48-
arguments in a list, while ``**`` does the same for keyword arguments
49-
in a dictionary.
43+
A value passed to a :term:`function` (or :term:`method`) when calling the
44+
function. There are two types of arguments:
45+
46+
* :dfn:`keyword argument`: an argument preceded by an identifier (e.g.
47+
``name=``) in a function call or passed as a value in a dictionary
48+
preceded by ``**``. For example, ``3`` and ``5`` are both keyword
49+
arguments in the following calls to :func:`complex`::
50+
51+
complex(real=3, imag=5)
52+
complex(**{'real': 3, 'imag': 5})
53+
54+
* :dfn:`positional argument`: an argument that is not a keyword argument.
55+
Positional arguments can appear at the beginning of an argument list
56+
and/or be passed as elements of an :term:`iterable` preceded by ``*``.
57+
For example, ``3`` and ``5`` are both positional arguments in the
58+
following calls::
59+
60+
complex(3, 5)
61+
complex(*(3, 5))
5062

51-
Any expression may be used within the argument list, and the evaluated
52-
value is passed to the local variable.
63+
Arguments are assigned to the named local variables in a function body.
64+
See the :ref:`calls` section for the rules governing this assignment.
65+
Syntactically, any expression can be used to represent an argument; the
66+
evaluated value is assigned to the local variable.
67+
68+
See also the :term:`parameter` glossary entry, the FAQ question on
69+
:ref:`the difference between arguments and parameters
70+
<faq-argument-vs-parameter>`, and :pep:`362`.
5371

5472
attribute
5573
A value associated with an object which is referenced by name using
@@ -391,10 +409,7 @@ Glossary
391409
<sortinghowto>` for examples of how to create and use key functions.
392410

393411
keyword argument
394-
Arguments which are preceded with a ``variable_name=`` in the call.
395-
The variable name designates the local name in the function to which the
396-
value is assigned. ``**`` is used to accept or pass a dictionary of
397-
keyword arguments. See :term:`argument`.
412+
See :term:`argument`.
398413

399414
lambda
400415
An anonymous inline function consisting of a single :term:`expression`
@@ -516,12 +531,55 @@ Glossary
516531
(methods). Also the ultimate base class of any :term:`new-style
517532
class`.
518533

534+
parameter
535+
A named entity in a :term:`function` (or method) definition that
536+
specifies an :term:`argument` (or in some cases, arguments) that the
537+
function can accept. There are five types of parameters:
538+
539+
* :dfn:`positional-or-keyword`: specifies an argument that can be passed
540+
either :term:`positionally <argument>` or as a :term:`keyword argument
541+
<argument>`. This is the default kind of parameter, for example *foo*
542+
and *bar* in the following::
543+
544+
def func(foo, bar=None): ...
545+
546+
* :dfn:`positional-only`: specifies an argument that can be supplied only
547+
by position. Python has no syntax for defining positional-only
548+
parameters. However, some built-in functions have positional-only
549+
parameters (e.g. :func:`abs`).
550+
551+
* :dfn:`keyword-only`: specifies an argument that can be supplied only
552+
by keyword. Keyword-only parameters can be defined by including a
553+
single var-positional parameter or bare ``*`` in the parameter list
554+
of the function definition before them, for example *kw_only1* and
555+
*kw_only2* in the following::
556+
557+
def func(arg, *, kw_only1, kw_only2): ...
558+
559+
* :dfn:`var-positional`: specifies that an arbitrary sequence of
560+
positional arguments can be provided (in addition to any positional
561+
arguments already accepted by other parameters). Such a parameter can
562+
be defined by prepending the parameter name with ``*``, for example
563+
*args* in the following::
564+
565+
def func(*args, **kwargs): ...
566+
567+
* :dfn:`var-keyword`: specifies that arbitrarily many keyword arguments
568+
can be provided (in addition to any keyword arguments already accepted
569+
by other parameters). Such a parameter can be defined by prepending
570+
the parameter name with ``**``, for example *kwargs* in the example
571+
above.
572+
573+
Parameters can specify both optional and required arguments, as well as
574+
default values for some optional arguments.
575+
576+
See also the :term:`argument` glossary entry, the FAQ question on
577+
:ref:`the difference between arguments and parameters
578+
<faq-argument-vs-parameter>`, the :class:`inspect.Parameter` class, the
579+
:ref:`function` section, and :pep:`362`.
580+
519581
positional argument
520-
The arguments assigned to local names inside a function or method,
521-
determined by the order in which they were given in the call. ``*`` is
522-
used to either accept multiple positional arguments (when in the
523-
definition), or pass several arguments as a list to a function. See
524-
:term:`argument`.
582+
See :term:`argument`.
525583

526584
Python 3000
527585
Nickname for the Python 3.x release line (coined long ago when the release

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,8 @@ Tools/Demos
741741
Documentation
742742
-------------
743743

744+
- Issue #15990: Improve argument/parameter documentation.
745+
744746
- Issue #13538: Improve str() and object.__str__() documentation.
745747

746748
- Issue #16400: Update the description of which versions of a given package

0 commit comments

Comments
 (0)