@@ -519,6 +519,8 @@ These are the types to which the function call operation (see section
519
519
:ref: `calls `) can be applied:
520
520
521
521
522
+ .. _user-defined-funcs :
523
+
522
524
User-defined functions
523
525
^^^^^^^^^^^^^^^^^^^^^^
524
526
@@ -647,43 +649,64 @@ callable object (normally a user-defined function).
647
649
single: __name__ (method attribute)
648
650
single: __module__ (method attribute)
649
651
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.
655
674
656
675
Methods 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 >` .
658
677
659
678
User-defined method objects may be created when getting an attribute of a
660
679
class (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 :
662
684
663
685
When 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
672
694
underlying the class method.
673
695
674
696
When 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
677
699
:class: `!C ` is a class which contains a definition for a function
678
700
:meth: `!f `, and ``x `` is an instance of :class: `!C `, calling ``x.f(1) `` is
679
701
equivalent to calling ``C.f(x, 1) ``.
680
702
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
683
705
itself, so that calling either ``x.f(1) `` or ``C.f(1) `` is equivalent to
684
706
calling ``f(C,1) `` where ``f `` is the underlying function.
685
707
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
687
710
object happens each time the attribute is retrieved from the instance. In
688
711
some cases, a fruitful optimization is to assign the attribute to a local
689
712
variable 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
767
790
the module the function was defined in or ``None `` if unavailable.
768
791
769
792
793
+ .. _builtin-methods :
794
+
770
795
Built-in methods
771
796
^^^^^^^^^^^^^^^^
772
797
@@ -778,8 +803,9 @@ Built-in methods
778
803
This is really a different disguise of a built-in function, this time containing
779
804
an object passed to the C function as an implicit extra argument. An example of
780
805
a 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__> `.)
783
809
784
810
785
811
Classes
@@ -894,8 +920,9 @@ https://www.python.org/download/releases/2.3/mro/.
894
920
895
921
When a class attribute reference (for class :class: `!C `, say) would yield a
896
922
class 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
899
926
object. See section :ref: `descriptors ` for another way in which attributes
900
927
retrieved from a class may differ from those actually contained in its
901
928
:attr: `~object.__dict__ `.
@@ -958,7 +985,7 @@ in which attribute references are searched. When an attribute is not found
958
985
there, and the instance's class has an attribute by that name, the search
959
986
continues with the class attributes. If a class attribute is found that is a
960
987
user-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
962
989
class method objects are also transformed; see above under "Classes". See
963
990
section :ref: `descriptors ` for another way in which attributes of a class
964
991
retrieved via its instances may differ from the objects actually stored in
0 commit comments