Skip to content

Commit e527059

Browse files
AlexWaygoodhugovk
authored andcommitted
pythongh-101100: Improve documentation on function attributes (python#112933)
Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent 718542a commit e527059

14 files changed

+159
-130
lines changed

Doc/c-api/function.rst

Lines changed: 13 additions & 10 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,8 @@ Using the non-data descriptor protocol, a pure Python version of
13421342
The :func:`functools.update_wrapper` call adds a ``__wrapped__`` attribute
13431343
that refers to the underlying function. Also it carries forward
13441344
the attributes necessary to make the wrapper look like the wrapped
1345-
function: ``__name__``, ``__qualname__``, ``__doc__``, and ``__annotations__``.
1345+
function: :attr:`~function.__name__`, :attr:`~function.__qualname__`,
1346+
:attr:`~function.__doc__`, and :attr:`~function.__annotations__`.
13461347

13471348
.. testcode::
13481349
:hide:
@@ -1522,8 +1523,9 @@ Using the non-data descriptor protocol, a pure Python version of
15221523
The :func:`functools.update_wrapper` call in ``ClassMethod`` adds a
15231524
``__wrapped__`` attribute that refers to the underlying function. Also
15241525
it carries forward the attributes necessary to make the wrapper look
1525-
like the wrapped function: ``__name__``, ``__qualname__``, ``__doc__``,
1526-
and ``__annotations__``.
1526+
like the wrapped function: :attr:`~function.__name__`,
1527+
:attr:`~function.__qualname__`, :attr:`~function.__doc__`,
1528+
and :attr:`~function.__annotations__`.
15271529

15281530

15291531
Member objects and __slots__

Doc/library/inspect.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,9 +1225,10 @@ Classes and functions
12251225
* If ``obj`` is a class, ``globals`` defaults to
12261226
``sys.modules[obj.__module__].__dict__`` and ``locals`` defaults
12271227
to the ``obj`` class namespace.
1228-
* If ``obj`` is a callable, ``globals`` defaults to ``obj.__globals__``,
1228+
* If ``obj`` is a callable, ``globals`` defaults to
1229+
:attr:`obj.__globals__ <function.__globals__>`,
12291230
although if ``obj`` is a wrapped function (using
1230-
``functools.update_wrapper()``) it is first unwrapped.
1231+
:func:`functools.update_wrapper`) it is first unwrapped.
12311232

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

Doc/library/stdtypes.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5384,10 +5384,10 @@ Code objects are used by the implementation to represent "pseudo-compiled"
53845384
executable Python code such as a function body. They differ from function
53855385
objects because they don't contain a reference to their global execution
53865386
environment. Code objects are returned by the built-in :func:`compile` function
5387-
and can be extracted from function objects through their :attr:`__code__`
5388-
attribute. See also the :mod:`code` module.
5387+
and can be extracted from function objects through their
5388+
:attr:`~function.__code__` attribute. See also the :mod:`code` module.
53895389

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

53935393
.. index::

Doc/library/xmlrpc.server.rst

Lines changed: 4 additions & 4 deletions
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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,8 @@ except that the original function is not temporarily bound to the name ``func``.
12611261
A list of :ref:`type parameters <type-params>` may be given in square brackets
12621262
between the function's name and the opening parenthesis for its parameter list.
12631263
This indicates to static type checkers that the function is generic. At runtime,
1264-
the type parameters can be retrieved from the function's ``__type_params__``
1264+
the type parameters can be retrieved from the function's
1265+
:attr:`~function.__type_params__`
12651266
attribute. See :ref:`generic-functions` for more.
12661267

12671268
.. versionchanged:: 3.12
@@ -1868,8 +1869,8 @@ like ``TYPE_PARAMS_OF_ListOrSet`` are not actually bound at runtime.
18681869
are mappings.
18691870
18701871
.. [#] A string literal appearing as the first statement in the function body is
1871-
transformed into the function's ``__doc__`` attribute and therefore the
1872-
function's :term:`docstring`.
1872+
transformed into the function's :attr:`~function.__doc__` attribute and
1873+
therefore the function's :term:`docstring`.
18731874
18741875
.. [#] A string literal appearing as the first statement in the class body is
18751876
transformed into the namespace's ``__doc__`` item and therefore the class's

0 commit comments

Comments
 (0)