Skip to content

Commit db79645

Browse files
AlexWaygoodaisk
authored andcommitted
pythongh-101100: Improve documentation for attributes on instance methods (python#112832)
1 parent c88d6e8 commit db79645

File tree

6 files changed

+76
-41
lines changed

6 files changed

+76
-41
lines changed

Doc/library/inspect.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
492492
Methods implemented via descriptors that also pass one of the other tests
493493
return ``False`` from the :func:`ismethoddescriptor` test, simply because the
494494
other tests promise more -- you can, e.g., count on having the
495-
:ref:`__func__ <instance-methods>` attribute (etc) when an object passes
495+
:attr:`~method.__func__` attribute (etc) when an object passes
496496
:func:`ismethod`.
497497

498498

Doc/library/stdtypes.rst

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5328,25 +5328,30 @@ Methods
53285328
.. index:: pair: object; method
53295329

53305330
Methods are functions that are called using the attribute notation. There are
5331-
two flavors: built-in methods (such as :meth:`append` on lists) and class
5332-
instance methods. Built-in methods are described with the types that support
5333-
them.
5331+
two flavors: :ref:`built-in methods <builtin-methods>` (such as :meth:`append`
5332+
on lists) and :ref:`class instance method <instance-methods>`.
5333+
Built-in methods are described with the types that support them.
53345334

53355335
If you access a method (a function defined in a class namespace) through an
53365336
instance, you get a special object: a :dfn:`bound method` (also called
5337-
:dfn:`instance method`) object. When called, it will add the ``self`` argument
5337+
:ref:`instance method <instance-methods>`) object. When called, it will add
5338+
the ``self`` argument
53385339
to the argument list. Bound methods have two special read-only attributes:
5339-
``m.__self__`` is the object on which the method operates, and ``m.__func__`` is
5340+
:attr:`m.__self__ <method.__self__>` is the object on which the method
5341+
operates, and :attr:`m.__func__ <method.__func__>` is
53405342
the function implementing the method. Calling ``m(arg-1, arg-2, ..., arg-n)``
53415343
is completely equivalent to calling ``m.__func__(m.__self__, arg-1, arg-2, ...,
53425344
arg-n)``.
53435345

5344-
Like function objects, bound method objects support getting arbitrary
5346+
Like :ref:`function objects <user-defined-funcs>`, bound method objects support
5347+
getting arbitrary
53455348
attributes. However, since method attributes are actually stored on the
5346-
underlying function object (``meth.__func__``), setting method attributes on
5349+
underlying function object (:attr:`method.__func__`), setting method attributes on
53475350
bound methods is disallowed. Attempting to set an attribute on a method
53485351
results in an :exc:`AttributeError` being raised. In order to set a method
5349-
attribute, you need to explicitly set it on the underlying function object::
5352+
attribute, you need to explicitly set it on the underlying function object:
5353+
5354+
.. doctest::
53505355

53515356
>>> class C:
53525357
... def method(self):
@@ -5361,7 +5366,7 @@ attribute, you need to explicitly set it on the underlying function object::
53615366
>>> c.method.whoami
53625367
'my name is method'
53635368

5364-
See :ref:`types` for more information.
5369+
See :ref:`instance-methods` for more information.
53655370

53665371

53675372
.. index:: object; code, code object

Doc/reference/datamodel.rst

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,8 @@ These are the types to which the function call operation (see section
519519
:ref:`calls`) can be applied:
520520

521521

522+
.. _user-defined-funcs:
523+
522524
User-defined functions
523525
^^^^^^^^^^^^^^^^^^^^^^
524526

@@ -654,43 +656,64 @@ callable object (normally a user-defined function).
654656
single: __name__ (method attribute)
655657
single: __module__ (method attribute)
656658

657-
Special read-only attributes: :attr:`__self__` is the class instance object,
658-
:attr:`__func__` is the function object; :attr:`__doc__` is the method's
659-
documentation (same as ``__func__.__doc__``); :attr:`~definition.__name__` is the
660-
method name (same as ``__func__.__name__``); :attr:`__module__` is the
661-
name of the module the method was defined in, or ``None`` if unavailable.
659+
Special read-only attributes:
660+
661+
.. list-table::
662+
663+
* - .. attribute:: method.__self__
664+
- Refers to the class instance object to which the method is
665+
:ref:`bound <method-binding>`
666+
667+
* - .. attribute:: method.__func__
668+
- Refers to the original function object
669+
670+
* - .. attribute:: method.__doc__
671+
- The method's documentation (same as :attr:`!method.__func__.__doc__`).
672+
A :class:`string <str>` if the original function had a docstring, else
673+
``None``.
674+
675+
* - .. attribute:: method.__name__
676+
- The name of the method (same as :attr:`!method.__func__.__name__`)
677+
678+
* - .. attribute:: method.__module__
679+
- The name of the module the method was defined in, or ``None`` if
680+
unavailable.
662681

663682
Methods also support accessing (but not setting) the arbitrary function
664-
attributes on the underlying function object.
683+
attributes on the underlying :ref:`function object <user-defined-funcs>`.
665684

666685
User-defined method objects may be created when getting an attribute of a
667686
class (perhaps via an instance of that class), if that attribute is a
668-
user-defined function object or a class method object.
687+
user-defined :ref:`function object <user-defined-funcs>` or a
688+
:class:`classmethod` object.
689+
690+
.. _method-binding:
669691

670692
When an instance method object is created by retrieving a user-defined
671-
function object from a class via one of its instances, its
672-
:attr:`__self__` attribute is the instance, and the method object is said
673-
to be bound. The new method's :attr:`__func__` attribute is the original
674-
function object.
675-
676-
When an instance method object is created by retrieving a class method
677-
object from a class or instance, its :attr:`__self__` attribute is the
678-
class itself, and its :attr:`__func__` attribute is the function object
693+
:ref:`function object <user-defined-funcs>` from a class via one of its
694+
instances, its :attr:`~method.__self__` attribute is the instance, and the
695+
method object is said to be *bound*. The new method's :attr:`~method.__func__`
696+
attribute is the original function object.
697+
698+
When an instance method object is created by retrieving a :class:`classmethod`
699+
object from a class or instance, its :attr:`~method.__self__` attribute is the
700+
class itself, and its :attr:`~method.__func__` attribute is the function object
679701
underlying the class method.
680702

681703
When an instance method object is called, the underlying function
682-
(:attr:`__func__`) is called, inserting the class instance
683-
(:attr:`__self__`) in front of the argument list. For instance, when
704+
(:attr:`~method.__func__`) is called, inserting the class instance
705+
(:attr:`~method.__self__`) in front of the argument list. For instance, when
684706
:class:`!C` is a class which contains a definition for a function
685707
:meth:`!f`, and ``x`` is an instance of :class:`!C`, calling ``x.f(1)`` is
686708
equivalent to calling ``C.f(x, 1)``.
687709

688-
When an instance method object is derived from a class method object, the
689-
"class instance" stored in :attr:`__self__` will actually be the class
710+
When an instance method object is derived from a :class:`classmethod` object, the
711+
"class instance" stored in :attr:`~method.__self__` will actually be the class
690712
itself, so that calling either ``x.f(1)`` or ``C.f(1)`` is equivalent to
691713
calling ``f(C,1)`` where ``f`` is the underlying function.
692714

693-
Note that the transformation from function object to instance method
715+
Note that the transformation from :ref:`function object <user-defined-funcs>`
716+
to instance method
694717
object happens each time the attribute is retrieved from the instance. In
695718
some cases, a fruitful optimization is to assign the attribute to a local
696719
variable and call that local variable. Also notice that this
@@ -774,6 +797,8 @@ set to ``None`` (but see the next item); :attr:`__module__` is the name of
774797
the module the function was defined in or ``None`` if unavailable.
775798

776799

800+
.. _builtin-methods:
801+
777802
Built-in methods
778803
^^^^^^^^^^^^^^^^
779804

@@ -785,8 +810,9 @@ Built-in methods
785810
This is really a different disguise of a built-in function, this time containing
786811
an object passed to the C function as an implicit extra argument. An example of
787812
a built-in method is ``alist.append()``, assuming *alist* is a list object. In
788-
this case, the special read-only attribute :attr:`__self__` is set to the object
789-
denoted by *alist*.
813+
this case, the special read-only attribute :attr:`!__self__` is set to the object
814+
denoted by *alist*. (The attribute has the same semantics as it does with
815+
:attr:`other instance methods <method.__self__>`.)
790816

791817

792818
Classes
@@ -901,8 +927,9 @@ https://www.python.org/download/releases/2.3/mro/.
901927

902928
When a class attribute reference (for class :class:`!C`, say) would yield a
903929
class method object, it is transformed into an instance method object whose
904-
:attr:`__self__` attribute is :class:`!C`. When it would yield a static
905-
method object, it is transformed into the object wrapped by the static method
930+
:attr:`~method.__self__` attribute is :class:`!C`.
931+
When it would yield a :class:`staticmethod` object,
932+
it is transformed into the object wrapped by the static method
906933
object. See section :ref:`descriptors` for another way in which attributes
907934
retrieved from a class may differ from those actually contained in its
908935
:attr:`~object.__dict__`.
@@ -970,7 +997,7 @@ in which attribute references are searched. When an attribute is not found
970997
there, and the instance's class has an attribute by that name, the search
971998
continues with the class attributes. If a class attribute is found that is a
972999
user-defined function object, it is transformed into an instance method
973-
object whose :attr:`__self__` attribute is the instance. Static method and
1000+
object whose :attr:`~method.__self__` attribute is the instance. Static method and
9741001
class method objects are also transformed; see above under "Classes". See
9751002
section :ref:`descriptors` for another way in which attributes of a class
9761003
retrieved via its instances may differ from the objects actually stored in

Doc/tutorial/classes.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,8 +769,10 @@ data from a string buffer instead, and pass it as an argument.
769769
or arithmetic operators, and assigning such a "pseudo-file" to sys.stdin will
770770
not cause the interpreter to read further input from it.)
771771
772-
Instance method objects have attributes, too: ``m.__self__`` is the instance
773-
object with the method :meth:`!m`, and ``m.__func__`` is the function object
772+
:ref:`Instance method objects <instance-methods>` have attributes, too:
773+
:attr:`m.__self__ <method.__self__>` is the instance
774+
object with the method :meth:`!m`, and :attr:`m.__func__ <method.__func__>` is
775+
the :ref:`function object <user-defined-funcs>`
774776
corresponding to the method.
775777

776778

Doc/whatsnew/2.6.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,8 +1678,8 @@ Some smaller changes made to the core Python language are:
16781678

16791679
* Instance method objects have new attributes for the object and function
16801680
comprising the method; the new synonym for :attr:`!im_self` is
1681-
:ref:`__self__ <instance-methods>`, and :attr:`!im_func` is also available as
1682-
:ref:`__func__ <instance-methods>`.
1681+
:attr:`~method.__self__`, and :attr:`!im_func` is also available as
1682+
:attr:`~method.__func__`.
16831683
The old names are still supported in Python 2.6, but are gone in 3.0.
16841684

16851685
* An obscure change: when you use the :func:`locals` function inside a

Doc/whatsnew/2.7.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,9 +858,10 @@ Some smaller changes made to the core Python language are:
858858

859859
.. XXX bytearray doesn't seem to be documented
860860
861-
* When using ``@classmethod`` and ``@staticmethod`` to wrap
861+
* When using :class:`@classmethod <classmethod>` and
862+
:class:`@staticmethod <staticmethod>` to wrap
862863
methods as class or static methods, the wrapper object now
863-
exposes the wrapped function as their :ref:`__func__ <instance-methods>`
864+
exposes the wrapped function as their :attr:`~method.__func__`
864865
attribute.
865866
(Contributed by Amaury Forgeot d'Arc, after a suggestion by
866867
George Sakkis; :issue:`5982`.)

0 commit comments

Comments
 (0)