Skip to content

Commit f83087c

Browse files
AlexWaygoodhugovk
andauthored
[3.11] gh-101100: Improve documentation on function attributes (#112933) (#113003)
Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent ada2d77 commit f83087c

13 files changed

+152
-122
lines changed

Doc/c-api/function.rst

+13-10
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,20 @@ There are a few functions specific to Python functions.
3434
Return a new function object associated with the code object *code*. *globals*
3535
must be a dictionary with the global variables accessible to the function.
3636
37-
The function's docstring and name are retrieved from the code object. *__module__*
37+
The function's docstring and name are retrieved from the code object.
38+
:func:`~function.__module__`
3839
is retrieved from *globals*. The argument defaults, annotations and closure are
39-
set to ``NULL``. *__qualname__* is set to the same value as the code object's
40-
:attr:`~codeobject.co_qualname` field.
40+
set to ``NULL``. :attr:`~function.__qualname__` is set to the same value as
41+
the code object's :attr:`~codeobject.co_qualname` field.
4142
4243
4344
.. c:function:: PyObject* PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
4445
4546
As :c:func:`PyFunction_New`, but also allows setting the function object's
46-
``__qualname__`` attribute. *qualname* should be a unicode object or ``NULL``;
47-
if ``NULL``, the ``__qualname__`` attribute is set to the same value as the
48-
code object's :attr:`~codeobject.co_qualname` field.
47+
:attr:`~function.__qualname__` attribute.
48+
*qualname* should be a unicode object or ``NULL``;
49+
if ``NULL``, the :attr:`!__qualname__` attribute is set to the same value as
50+
the code object's :attr:`~codeobject.co_qualname` field.
4951
5052
.. versionadded:: 3.3
5153
@@ -62,11 +64,12 @@ There are a few functions specific to Python functions.
6264
6365
.. c:function:: PyObject* PyFunction_GetModule(PyObject *op)
6466
65-
Return a :term:`borrowed reference` to the *__module__* attribute of the
66-
function object *op*. It can be *NULL*.
67+
Return a :term:`borrowed reference` to the :attr:`~function.__module__`
68+
attribute of the :ref:`function object <user-defined-funcs>` *op*.
69+
It can be *NULL*.
6770
68-
This is normally a string containing the module name, but can be set to any
69-
other object by Python code.
71+
This is normally a :class:`string <str>` containing the module name,
72+
but can be set to any other object by Python code.
7073
7174
7275
.. c:function:: PyObject* PyFunction_GetDefaults(PyObject *op)

Doc/howto/annotations.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ on an arbitrary object ``o``:
153153
unwrap it by accessing either ``o.__wrapped__`` or ``o.func`` as
154154
appropriate, until you have found the root unwrapped function.
155155
* If ``o`` is a callable (but not a class), use
156-
``o.__globals__`` as the globals when calling :func:`eval`.
156+
:attr:`o.__globals__ <function.__globals__>` as the globals when calling
157+
:func:`eval`.
157158

158159
However, not all string values used as annotations can
159160
be successfully turned into Python values by :func:`eval`.

Doc/howto/descriptor.rst

+5-3
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,8 @@ Using the non-data descriptor protocol, a pure Python version of
12951295
The :func:`functools.update_wrapper` call adds a ``__wrapped__`` attribute
12961296
that refers to the underlying function. Also it carries forward
12971297
the attributes necessary to make the wrapper look like the wrapped
1298-
function: ``__name__``, ``__qualname__``, ``__doc__``, and ``__annotations__``.
1298+
function: :attr:`~function.__name__`, :attr:`~function.__qualname__`,
1299+
:attr:`~function.__doc__`, and :attr:`~function.__annotations__`.
12991300

13001301
.. testcode::
13011302
:hide:
@@ -1506,8 +1507,9 @@ chained together. In Python 3.11, this functionality was deprecated.
15061507
The :func:`functools.update_wrapper` call in ``ClassMethod`` adds a
15071508
``__wrapped__`` attribute that refers to the underlying function. Also
15081509
it carries forward the attributes necessary to make the wrapper look
1509-
like the wrapped function: ``__name__``, ``__qualname__``, ``__doc__``,
1510-
and ``__annotations__``.
1510+
like the wrapped function: :attr:`~function.__name__`,
1511+
:attr:`~function.__qualname__`, :attr:`~function.__doc__`,
1512+
and :attr:`~function.__annotations__`.
15111513

15121514

15131515
Member objects and __slots__

Doc/library/inspect.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -1186,9 +1186,10 @@ Classes and functions
11861186
* If ``obj`` is a class, ``globals`` defaults to
11871187
``sys.modules[obj.__module__].__dict__`` and ``locals`` defaults
11881188
to the ``obj`` class namespace.
1189-
* If ``obj`` is a callable, ``globals`` defaults to ``obj.__globals__``,
1189+
* If ``obj`` is a callable, ``globals`` defaults to
1190+
:attr:`obj.__globals__ <function.__globals__>`,
11901191
although if ``obj`` is a wrapped function (using
1191-
``functools.update_wrapper()``) it is first unwrapped.
1192+
:func:`functools.update_wrapper`) it is first unwrapped.
11921193

11931194
Calling ``get_annotations`` is best practice for accessing the
11941195
annotations dict of any object. See :ref:`annotations-howto` for

Doc/library/stdtypes.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -5326,10 +5326,10 @@ Code objects are used by the implementation to represent "pseudo-compiled"
53265326
executable Python code such as a function body. They differ from function
53275327
objects because they don't contain a reference to their global execution
53285328
environment. Code objects are returned by the built-in :func:`compile` function
5329-
and can be extracted from function objects through their :attr:`__code__`
5330-
attribute. See also the :mod:`code` module.
5329+
and can be extracted from function objects through their
5330+
:attr:`~function.__code__` attribute. See also the :mod:`code` module.
53315331

5332-
Accessing ``__code__`` raises an :ref:`auditing event <auditing>`
5332+
Accessing :attr:`~function.__code__` raises an :ref:`auditing event <auditing>`
53335333
``object.__getattr__`` with arguments ``obj`` and ``"__code__"``.
53345334

53355335
.. index::

Doc/library/xmlrpc.server.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ alone XML-RPC servers.
8484

8585
Register a function that can respond to XML-RPC requests. If *name* is given,
8686
it will be the method name associated with *function*, otherwise
87-
``function.__name__`` will be used. *name* is a string, and may contain
87+
:attr:`function.__name__` will be used. *name* is a string, and may contain
8888
characters not legal in Python identifiers, including the period character.
8989

9090
This method can also be used as a decorator. When used as a decorator,
9191
*name* can only be given as a keyword argument to register *function* under
92-
*name*. If no *name* is given, ``function.__name__`` will be used.
92+
*name*. If no *name* is given, :attr:`function.__name__` will be used.
9393

9494
.. versionchanged:: 3.7
9595
:meth:`register_function` can be used as a decorator.
@@ -298,12 +298,12 @@ requests sent to Python CGI scripts.
298298

299299
Register a function that can respond to XML-RPC requests. If *name* is given,
300300
it will be the method name associated with *function*, otherwise
301-
``function.__name__`` will be used. *name* is a string, and may contain
301+
:attr:`function.__name__` will be used. *name* is a string, and may contain
302302
characters not legal in Python identifiers, including the period character.
303303

304304
This method can also be used as a decorator. When used as a decorator,
305305
*name* can only be given as a keyword argument to register *function* under
306-
*name*. If no *name* is given, ``function.__name__`` will be used.
306+
*name*. If no *name* is given, :attr:`function.__name__` will be used.
307307

308308
.. versionchanged:: 3.7
309309
:meth:`register_function` can be used as a decorator.

Doc/reference/compound_stmts.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -1630,8 +1630,8 @@ body of a coroutine function.
16301630
are mappings.
16311631
16321632
.. [#] A string literal appearing as the first statement in the function body is
1633-
transformed into the function's ``__doc__`` attribute and therefore the
1634-
function's :term:`docstring`.
1633+
transformed into the function's :attr:`~function.__doc__` attribute and
1634+
therefore the function's :term:`docstring`.
16351635
16361636
.. [#] A string literal appearing as the first statement in the class body is
16371637
transformed into the namespace's ``__doc__`` item and therefore the class's

Doc/reference/datamodel.rst

+103-84
Original file line numberDiff line numberDiff line change
@@ -534,99 +534,108 @@ section :ref:`function`). It should be called with an argument list
534534
containing the same number of items as the function's formal parameter
535535
list.
536536

537-
Special attributes:
537+
Special read-only attributes
538+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
539+
540+
.. index::
541+
single: __closure__ (function attribute)
542+
single: __globals__ (function attribute)
543+
pair: global; namespace
544+
545+
.. list-table::
546+
:header-rows: 1
538547

539-
.. tabularcolumns:: |l|L|l|
548+
* - Attribute
549+
- Meaning
550+
551+
* - .. attribute:: function.__globals__
552+
- A reference to the :class:`dictionary <dict>` that holds the function's
553+
:ref:`global variables <naming>` -- the global namespace of the module
554+
in which the function was defined.
555+
556+
* - .. attribute:: function.__closure__
557+
- ``None`` or a :class:`tuple` of cells that contain bindings for the
558+
function's free variables.
559+
560+
A cell object has the attribute ``cell_contents``.
561+
This can be used to get the value of the cell, as well as set the value.
562+
563+
Special writable attributes
564+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
540565

541566
.. index::
542567
single: __doc__ (function attribute)
543568
single: __name__ (function attribute)
544569
single: __module__ (function attribute)
545570
single: __dict__ (function attribute)
546571
single: __defaults__ (function attribute)
547-
single: __closure__ (function attribute)
548572
single: __code__ (function attribute)
549-
single: __globals__ (function attribute)
550573
single: __annotations__ (function attribute)
551574
single: __kwdefaults__ (function attribute)
552-
pair: global; namespace
553575

554-
+-------------------------+-------------------------------+-----------+
555-
| Attribute | Meaning | |
556-
+=========================+===============================+===========+
557-
| :attr:`__doc__` | The function's documentation | Writable |
558-
| | string, or ``None`` if | |
559-
| | unavailable; not inherited by | |
560-
| | subclasses. | |
561-
+-------------------------+-------------------------------+-----------+
562-
| :attr:`~definition.\ | The function's name. | Writable |
563-
| __name__` | | |
564-
+-------------------------+-------------------------------+-----------+
565-
| :attr:`~definition.\ | The function's | Writable |
566-
| __qualname__` | :term:`qualified name`. | |
567-
| | | |
568-
| | .. versionadded:: 3.3 | |
569-
+-------------------------+-------------------------------+-----------+
570-
| :attr:`__module__` | The name of the module the | Writable |
571-
| | function was defined in, or | |
572-
| | ``None`` if unavailable. | |
573-
+-------------------------+-------------------------------+-----------+
574-
| :attr:`__defaults__` | A tuple containing default | Writable |
575-
| | argument values for those | |
576-
| | arguments that have defaults, | |
577-
| | or ``None`` if no arguments | |
578-
| | have a default value. | |
579-
+-------------------------+-------------------------------+-----------+
580-
| :attr:`__code__` | The code object representing | Writable |
581-
| | the compiled function body. | |
582-
+-------------------------+-------------------------------+-----------+
583-
| :attr:`__globals__` | A reference to the dictionary | Read-only |
584-
| | that holds the function's | |
585-
| | global variables --- the | |
586-
| | global namespace of the | |
587-
| | module in which the function | |
588-
| | was defined. | |
589-
+-------------------------+-------------------------------+-----------+
590-
| :attr:`~object.__dict__`| The namespace supporting | Writable |
591-
| | arbitrary function | |
592-
| | attributes. | |
593-
+-------------------------+-------------------------------+-----------+
594-
| :attr:`__closure__` | ``None`` or a tuple of cells | Read-only |
595-
| | that contain bindings for the | |
596-
| | function's free variables. | |
597-
| | See below for information on | |
598-
| | the ``cell_contents`` | |
599-
| | attribute. | |
600-
+-------------------------+-------------------------------+-----------+
601-
| :attr:`__annotations__` | A dict containing annotations | Writable |
602-
| | of parameters. The keys of | |
603-
| | the dict are the parameter | |
604-
| | names, and ``'return'`` for | |
605-
| | the return annotation, if | |
606-
| | provided. For more | |
607-
| | information on working with | |
608-
| | this attribute, see | |
609-
| | :ref:`annotations-howto`. | |
610-
+-------------------------+-------------------------------+-----------+
611-
| :attr:`__kwdefaults__` | A dict containing defaults | Writable |
612-
| | for keyword-only parameters. | |
613-
+-------------------------+-------------------------------+-----------+
614-
615-
Most of the attributes labelled "Writable" check the type of the assigned value.
576+
Most of these attributes check the type of the assigned value:
577+
578+
.. list-table::
579+
:header-rows: 1
580+
581+
* - Attribute
582+
- Meaning
583+
584+
* - .. attribute:: function.__doc__
585+
- The function's documentation string, or ``None`` if unavailable.
586+
Not inherited by subclasses.
587+
588+
* - .. attribute:: function.__name__
589+
- The function's name.
590+
See also: :attr:`__name__ attributes <definition.__name__>`.
591+
592+
* - .. attribute:: function.__qualname__
593+
- The function's :term:`qualified name`.
594+
See also: :attr:`__qualname__ attributes <definition.__qualname__>`.
595+
596+
.. versionadded:: 3.3
597+
598+
* - .. attribute:: function.__module__
599+
- The name of the module the function was defined in,
600+
or ``None`` if unavailable.
601+
602+
* - .. attribute:: function.__defaults__
603+
- A :class:`tuple` containing default parameter values
604+
for those parameters that have defaults,
605+
or ``None`` if no parameters have a default value.
606+
607+
* - .. attribute:: function.__code__
608+
- The :ref:`code object <code-objects>` representing
609+
the compiled function body.
610+
611+
* - .. attribute:: function.__dict__
612+
- The namespace supporting arbitrary function attributes.
613+
See also: :attr:`__dict__ attributes <object.__dict__>`.
614+
615+
* - .. attribute:: function.__annotations__
616+
- A :class:`dictionary <dict>` containing annotations of parameters.
617+
The keys of the dictionary are the parameter names,
618+
and ``'return'`` for the return annotation, if provided.
619+
See also: :ref:`annotations-howto`.
620+
621+
* - .. attribute:: function.__kwdefaults__
622+
- A :class:`dictionary <dict>` containing defaults for keyword-only
623+
parameters.
616624

617625
Function objects also support getting and setting arbitrary attributes, which
618626
can be used, for example, to attach metadata to functions. Regular attribute
619-
dot-notation is used to get and set such attributes. *Note that the current
620-
implementation only supports function attributes on user-defined functions.
621-
Function attributes on built-in functions may be supported in the future.*
627+
dot-notation is used to get and set such attributes.
622628

623-
A cell object has the attribute ``cell_contents``. This can be used to get
624-
the value of the cell, as well as set the value.
629+
.. impl-detail::
630+
631+
CPython's current implementation only supports function attributes
632+
on user-defined functions. Function attributes on
633+
:ref:`built-in functions <builtin-functions>` may be supported in the
634+
future.
625635

626636
Additional information about a function's definition can be retrieved from its
627-
code object; see the description of internal types below. The
628-
:data:`cell <types.CellType>` type can be accessed in the :mod:`types`
629-
module.
637+
:ref:`code object <code-objects>`
638+
(accessible via the :attr:`~function.__code__` attribute).
630639

631640

632641
.. _instance-methods:
@@ -658,15 +667,17 @@ Special read-only attributes:
658667
:ref:`bound <method-binding>`
659668

660669
* - .. attribute:: method.__func__
661-
- Refers to the original function object
670+
- Refers to the original :ref:`function object <user-defined-funcs>`
662671

663672
* - .. attribute:: method.__doc__
664-
- The method's documentation (same as :attr:`!method.__func__.__doc__`).
673+
- The method's documentation
674+
(same as :attr:`method.__func__.__doc__ <function.__doc__>`).
665675
A :class:`string <str>` if the original function had a docstring, else
666676
``None``.
667677

668678
* - .. attribute:: method.__name__
669-
- The name of the method (same as :attr:`!method.__func__.__name__`)
679+
- The name of the method
680+
(same as :attr:`method.__func__.__name__ <function.__name__>`)
670681

671682
* - .. attribute:: method.__module__
672683
- The name of the module the method was defined in, or ``None`` if
@@ -772,6 +783,8 @@ is raised and the asynchronous iterator will have reached the end of
772783
the set of values to be yielded.
773784

774785

786+
.. _builtin-functions:
787+
775788
Built-in functions
776789
^^^^^^^^^^^^^^^^^^
777790

@@ -784,10 +797,14 @@ A built-in function object is a wrapper around a C function. Examples of
784797
built-in functions are :func:`len` and :func:`math.sin` (:mod:`math` is a
785798
standard built-in module). The number and type of the arguments are
786799
determined by the C function. Special read-only attributes:
787-
:attr:`__doc__` is the function's documentation string, or ``None`` if
788-
unavailable; :attr:`~definition.__name__` is the function's name; :attr:`__self__` is
789-
set to ``None`` (but see the next item); :attr:`__module__` is the name of
790-
the module the function was defined in or ``None`` if unavailable.
800+
801+
* :attr:`!__doc__` is the function's documentation string, or ``None`` if
802+
unavailable. See :attr:`function.__doc__`.
803+
* :attr:`!__name__` is the function's name. See :attr:`function.__name__`.
804+
* :attr:`!__self__` is set to ``None`` (but see the next item).
805+
* :attr:`!__module__` is the name of
806+
the module the function was defined in or ``None`` if unavailable.
807+
See :attr:`function.__module__`.
791808

792809

793810
.. _builtin-methods:
@@ -837,7 +854,8 @@ the :ref:`import system <importsystem>` as invoked either by the
837854
:keyword:`import` statement, or by calling
838855
functions such as :func:`importlib.import_module` and built-in
839856
:func:`__import__`. A module object has a namespace implemented by a
840-
dictionary object (this is the dictionary referenced by the ``__globals__``
857+
:class:`dictionary <dict>` object (this is the dictionary referenced by the
858+
:attr:`~function.__globals__`
841859
attribute of functions defined in the module). Attribute references are
842860
translated to lookups in this dictionary, e.g., ``m.x`` is equivalent to
843861
``m.__dict__["x"]``. A module object does not contain the code object used
@@ -1869,7 +1887,8 @@ access (use of, assignment to, or deletion of ``x.name``) for class instances.
18691887
.. note::
18701888

18711889
This method may still be bypassed when looking up special methods as the
1872-
result of implicit invocation via language syntax or built-in functions.
1890+
result of implicit invocation via language syntax or
1891+
:ref:`built-in functions <builtin-functions>`.
18731892
See :ref:`special-lookup`.
18741893

18751894
.. audit-event:: object.__getattr__ obj,name object.__getattribute__

0 commit comments

Comments
 (0)