@@ -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
@@ -654,43 +656,64 @@ callable object (normally a user-defined function).
654
656
single: __name__ (method attribute)
655
657
single: __module__ (method attribute)
656
658
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.
662
681
663
682
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 >` .
665
684
666
685
User-defined method objects may be created when getting an attribute of a
667
686
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 :
669
691
670
692
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
679
701
underlying the class method.
680
702
681
703
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
684
706
:class: `!C ` is a class which contains a definition for a function
685
707
:meth: `!f `, and ``x `` is an instance of :class: `!C `, calling ``x.f(1) `` is
686
708
equivalent to calling ``C.f(x, 1) ``.
687
709
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
690
712
itself, so that calling either ``x.f(1) `` or ``C.f(1) `` is equivalent to
691
713
calling ``f(C,1) `` where ``f `` is the underlying function.
692
714
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
694
717
object happens each time the attribute is retrieved from the instance. In
695
718
some cases, a fruitful optimization is to assign the attribute to a local
696
719
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
774
797
the module the function was defined in or ``None `` if unavailable.
775
798
776
799
800
+ .. _builtin-methods :
801
+
777
802
Built-in methods
778
803
^^^^^^^^^^^^^^^^
779
804
@@ -785,8 +810,9 @@ Built-in methods
785
810
This is really a different disguise of a built-in function, this time containing
786
811
an object passed to the C function as an implicit extra argument. An example of
787
812
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__> `.)
790
816
791
817
792
818
Classes
@@ -901,8 +927,9 @@ https://www.python.org/download/releases/2.3/mro/.
901
927
902
928
When a class attribute reference (for class :class: `!C `, say) would yield a
903
929
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
906
933
object. See section :ref: `descriptors ` for another way in which attributes
907
934
retrieved from a class may differ from those actually contained in its
908
935
:attr: `~object.__dict__ `.
@@ -970,7 +997,7 @@ in which attribute references are searched. When an attribute is not found
970
997
there, and the instance's class has an attribute by that name, the search
971
998
continues with the class attributes. If a class attribute is found that is a
972
999
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
974
1001
class method objects are also transformed; see above under "Classes". See
975
1002
section :ref: `descriptors ` for another way in which attributes of a class
976
1003
retrieved via its instances may differ from the objects actually stored in
0 commit comments