@@ -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+
522524User-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
663682Methods 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
666685User-defined method objects may be created when getting an attribute of a
667686class (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
670692When 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
679701underlying the class method.
680702
681703When 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
686708equivalent 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
690712itself, so that calling either ``x.f(1) `` or ``C.f(1) `` is equivalent to
691713calling ``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
694717object happens each time the attribute is retrieved from the instance. In
695718some cases, a fruitful optimization is to assign the attribute to a local
696719variable 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
774797the module the function was defined in or ``None `` if unavailable.
775798
776799
800+ .. _builtin-methods :
801+
777802Built-in methods
778803^^^^^^^^^^^^^^^^
779804
@@ -785,8 +810,9 @@ Built-in methods
785810This is really a different disguise of a built-in function, this time containing
786811an object passed to the C function as an implicit extra argument. An example of
787812a 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
792818Classes
@@ -901,8 +927,9 @@ https://www.python.org/download/releases/2.3/mro/.
901927
902928When a class attribute reference (for class :class: `!C `, say) would yield a
903929class 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
906933object. See section :ref: `descriptors ` for another way in which attributes
907934retrieved 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
970997there, and the instance's class has an attribute by that name, the search
971998continues with the class attributes. If a class attribute is found that is a
972999user-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
9741001class method objects are also transformed; see above under "Classes". See
9751002section :ref: `descriptors ` for another way in which attributes of a class
9761003retrieved via its instances may differ from the objects actually stored in
0 commit comments