@@ -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
@@ -647,43 +649,64 @@ callable object (normally a user-defined function).
647649 single: __name__ (method attribute)
648650 single: __module__ (method attribute)
649651
650- Special read-only attributes: :attr: `__self__ ` is the class instance object,
651- :attr: `__func__ ` is the function object; :attr: `__doc__ ` is the method's
652- documentation (same as ``__func__.__doc__ ``); :attr: `~definition.__name__ ` is the
653- method name (same as ``__func__.__name__ ``); :attr: `__module__ ` is the
654- name of the module the method was defined in, or ``None `` if unavailable.
652+ Special read-only attributes:
653+
654+ .. list-table ::
655+
656+ * - .. attribute:: method.__self__
657+ - Refers to the class instance object to which the method is
658+ :ref: `bound <method-binding >`
659+
660+ * - .. attribute:: method.__func__
661+ - Refers to the original function object
662+
663+ * - .. attribute:: method.__doc__
664+ - The method's documentation (same as :attr: `!method.__func__.__doc__ `).
665+ A :class: `string <str> ` if the original function had a docstring, else
666+ ``None ``.
667+
668+ * - .. attribute:: method.__name__
669+ - The name of the method (same as :attr: `!method.__func__.__name__ `)
670+
671+ * - .. attribute:: method.__module__
672+ - The name of the module the method was defined in, or ``None `` if
673+ unavailable.
655674
656675Methods also support accessing (but not setting) the arbitrary function
657- attributes on the underlying function object.
676+ attributes on the underlying :ref: ` function object < user-defined-funcs >` .
658677
659678User-defined method objects may be created when getting an attribute of a
660679class (perhaps via an instance of that class), if that attribute is a
661- user-defined function object or a class method object.
680+ user-defined :ref: `function object <user-defined-funcs >` or a
681+ :class: `classmethod ` object.
682+
683+ .. _method-binding :
662684
663685When an instance method object is created by retrieving a user-defined
664- function object from a class via one of its instances, its
665- :attr: `__self__ ` attribute is the instance, and the method object is said
666- to be bound. The new method's :attr: `__func__ ` attribute is the original
667- function object.
668-
669- When an instance method object is created by retrieving a class method
670- object from a class or instance, its :attr: `__self__ ` attribute is the
671- class itself, and its :attr: `__func__ ` attribute is the function object
686+ :ref: ` function object < user-defined-funcs >` from a class via one of its
687+ instances, its :attr: `~method. __self__ ` attribute is the instance, and the
688+ method object is said to be * bound * . The new method's :attr: `~method. __func__ `
689+ attribute is the original function object.
690+
691+ When an instance method object is created by retrieving a : class: ` classmethod `
692+ object from a class or instance, its :attr: `~method. __self__ ` attribute is the
693+ class itself, and its :attr: `~method. __func__ ` attribute is the function object
672694underlying the class method.
673695
674696When an instance method object is called, the underlying function
675- (:attr: `__func__ `) is called, inserting the class instance
676- (:attr: `__self__ `) in front of the argument list. For instance, when
697+ (:attr: `~method. __func__ `) is called, inserting the class instance
698+ (:attr: `~method. __self__ `) in front of the argument list. For instance, when
677699:class: `!C ` is a class which contains a definition for a function
678700:meth: `!f `, and ``x `` is an instance of :class: `!C `, calling ``x.f(1) `` is
679701equivalent to calling ``C.f(x, 1) ``.
680702
681- When an instance method object is derived from a class method object, the
682- "class instance" stored in :attr: `__self__ ` will actually be the class
703+ When an instance method object is derived from a : class: ` classmethod ` object, the
704+ "class instance" stored in :attr: `~method. __self__ ` will actually be the class
683705itself, so that calling either ``x.f(1) `` or ``C.f(1) `` is equivalent to
684706calling ``f(C,1) `` where ``f `` is the underlying function.
685707
686- Note that the transformation from function object to instance method
708+ Note that the transformation from :ref: `function object <user-defined-funcs >`
709+ to instance method
687710object happens each time the attribute is retrieved from the instance. In
688711some cases, a fruitful optimization is to assign the attribute to a local
689712variable and call that local variable. Also notice that this
@@ -767,6 +790,8 @@ set to ``None`` (but see the next item); :attr:`__module__` is the name of
767790the module the function was defined in or ``None `` if unavailable.
768791
769792
793+ .. _builtin-methods :
794+
770795Built-in methods
771796^^^^^^^^^^^^^^^^
772797
@@ -778,8 +803,9 @@ Built-in methods
778803This is really a different disguise of a built-in function, this time containing
779804an object passed to the C function as an implicit extra argument. An example of
780805a built-in method is ``alist.append() ``, assuming *alist * is a list object. In
781- this case, the special read-only attribute :attr: `__self__ ` is set to the object
782- denoted by *alist *.
806+ this case, the special read-only attribute :attr: `!__self__ ` is set to the object
807+ denoted by *alist *. (The attribute has the same semantics as it does with
808+ :attr: `other instance methods <method.__self__> `.)
783809
784810
785811Classes
@@ -894,8 +920,9 @@ https://www.python.org/download/releases/2.3/mro/.
894920
895921When a class attribute reference (for class :class: `!C `, say) would yield a
896922class method object, it is transformed into an instance method object whose
897- :attr: `__self__ ` attribute is :class: `!C `. When it would yield a static
898- method object, it is transformed into the object wrapped by the static method
923+ :attr: `~method.__self__ ` attribute is :class: `!C `.
924+ When it would yield a :class: `staticmethod ` object,
925+ it is transformed into the object wrapped by the static method
899926object. See section :ref: `descriptors ` for another way in which attributes
900927retrieved from a class may differ from those actually contained in its
901928:attr: `~object.__dict__ `.
@@ -958,7 +985,7 @@ in which attribute references are searched. When an attribute is not found
958985there, and the instance's class has an attribute by that name, the search
959986continues with the class attributes. If a class attribute is found that is a
960987user-defined function object, it is transformed into an instance method
961- object whose :attr: `__self__ ` attribute is the instance. Static method and
988+ object whose :attr: `~method. __self__ ` attribute is the instance. Static method and
962989class method objects are also transformed; see above under "Classes". See
963990section :ref: `descriptors ` for another way in which attributes of a class
964991retrieved via its instances may differ from the objects actually stored in
0 commit comments