Removed the API to create unbound methods and simplified the API for … · python/cpython@ff73795 (original) (raw)
`@@ -538,20 +538,18 @@ Callable types
`
538
538
``` A user-defined method object combines a class, a class instance (or None
)
`539`
`539`
` and any callable object (normally a user-defined function).
`
`540`
`540`
``
`541`
``
`` -
Special read-only attributes: :attr:`im_self` is the class instance object,
``
`542`
``
`` -
:attr:`im_func` is the function object; :attr:`im_class` is the class of
``
`543`
``
`` -
:attr:`im_self` for bound methods or the class that asked for the method for
``
`544`
``
`` -
unbound methods; :attr:`__doc__` is the method's documentation (same as
``
`545`
``
``` -
``im_func.__doc__``); :attr:`__name__` is the method name (same as
546
``
``im_func.__name__``); :attr:`__module__` is the name of the module the method
547
``
was defined in, or ``None`` if unavailable.
``
541
`` +
Special read-only attributes: :attr:__self__
is the class instance object,
``
``
542
`` +
:attr:__func__
is the function object; :attr:__doc__
is the method's
``
``
543
documentation (same as ``__func__.__doc__``); :attr:`__name__` is the
``
544
method name (same as ``__func__.__name__``); :attr:`__module__` is the
``
545
name of the module the method was defined in, or ``None`` if unavailable.
548
546
``
549
547
` .. index::
`
550
548
` single: doc (method attribute)
`
551
549
` single: name (method attribute)
`
552
550
` single: module (method attribute)
`
553
``
`-
single: im_func (method attribute)
`
554
``
`-
single: im_self (method attribute)
`
``
551
`+
single: func (method attribute)
`
``
552
`+
single: self (method attribute)
`
555
553
``
556
554
` Methods also support accessing (but not setting) the arbitrary function
`
557
555
` attributes on the underlying function object.
`
`@@ -565,49 +563,46 @@ Callable types
`
565
563
` the original method object is used as it is.
`
566
564
``
567
565
` .. index::
`
568
``
`-
single: im_class (method attribute)
`
569
``
`-
single: im_func (method attribute)
`
570
``
`-
single: im_self (method attribute)
`
``
566
`+
single: func (method attribute)
`
``
567
`+
single: self (method attribute)
`
571
568
``
572
569
` When a user-defined method object is created by retrieving a user-defined
`
573
``
function object from a class, its :attr:`im_self` attribute is ``None``
``
570
function object from a class, its :attr:`__self__` attribute is ``None``
574
571
` and the method object is said to be unbound. When one is created by
`
575
572
` retrieving a user-defined function object from a class via one of its
`
576
``
`` -
instances, its :attr:im_self
attribute is the instance, and the method
``
577
``
`-
object is said to be bound. In either case, the new method's
`
578
``
`` -
:attr:im_class
attribute is the class from which the retrieval takes
``
579
``
`` -
place, and its :attr:im_func
attribute is the original function object.
``
``
573
`` +
instances, its :attr:__self__
attribute is the instance, and the method
``
``
574
`` +
object is said to be bound. Its :attr:__func__
attribute is the
``
``
575
`+
original function object.
`
580
576
``
581
``
`-
.. index:: single: im_func (method attribute)
`
``
577
`+
.. index:: single: func (method attribute)
`
582
578
``
583
579
` When a user-defined method object is created by retrieving another method object
`
584
580
` from a class or instance, the behaviour is the same as for a function object,
`
585
``
`` -
except that the :attr:im_func
attribute of the new instance is not the
``
586
``
`` -
original method object but its :attr:im_func
attribute.
``
``
581
`` +
except that the :attr:__func__
attribute of the new instance is not the
``
``
582
`` +
original method object but its :attr:__func__
attribute.
``
587
583
``
588
584
` .. index::
`
589
``
`-
single: im_class (method attribute)
`
590
``
`-
single: im_func (method attribute)
`
591
``
`-
single: im_self (method attribute)
`
``
585
`+
single: func (method attribute)
`
``
586
`+
single: self (method attribute)
`
592
587
``
593
588
` When a user-defined method object is created by retrieving a class method object
`
594
``
`` -
from a class or instance, its :attr:im_self
attribute is the class itself (the
``
595
``
`` -
same as the :attr:im_class
attribute), and its :attr:im_func
attribute is
``
``
589
`` +
from a class or instance, its :attr:__self__
attribute is the class itself (the
``
``
590
`` +
same as the :attr:im_class
attribute), and its :attr:__func__
attribute is
``
596
591
` the function object underlying the class method.
`
597
592
``
598
593
` When an unbound user-defined method object is called, the underlying function
`
599
``
`` -
(:attr:im_func
) is called, with the restriction that the first argument must
``
``
594
`` +
(:attr:__func__
) is called, with the restriction that the first argument must
``
600
595
`` be an instance of the proper class (:attr:im_class
) or of a derived class
``
601
596
` thereof.
`
602
597
``
603
598
` When a bound user-defined method object is called, the underlying function
`
604
``
`` -
(:attr:im_func
) is called, inserting the class instance (:attr:im_self
) in
``
``
599
`` +
(:attr:__func__
) is called, inserting the class instance (:attr:__self__
) in
``
605
600
`` front of the argument list. For instance, when :class:C
is a class which
``
606
601
``` contains a definition for a function :meth:f
, and x
is an instance of
`607`
`602`
``` :class:`C`, calling ``x.f(1)`` is equivalent to calling ``C.f(x, 1)``.
608
603
``
609
604
` When a user-defined method object is derived from a class method object, the
`
610
``
`` -
"class instance" stored in :attr:im_self
will actually be the class itself, so
``
``
605
`` +
"class instance" stored in :attr:__self__
will actually be the class itself, so
``
611
606
``` that calling either x.f(1)
or C.f(1)
is equivalent to calling f(C,1)
`612`
`607`
``` where ``f`` is the underlying function.
613
608
``
`@@ -741,7 +736,7 @@ Custom classes
`
741
736
`` transformed into an unbound user-defined method object whose :attr:im_class
``
742
737
`` attribute is :class:C
. When it would yield a class method object, it is
``
743
738
`` transformed into a bound user-defined method object whose :attr:im_class
``
744
``
`` -
and :attr:im_self
attributes are both :class:C
. When it would yield a
``
``
739
`` +
and :attr:__self__
attributes are both :class:C
. When it would yield a
``
745
740
` static method object, it is transformed into the object wrapped by the static
`
746
741
`` method object. See section :ref:descriptors
for another way in which
``
747
742
` attributes retrieved from a class may differ from those actually contained in
`
`@@ -786,7 +781,7 @@ Class instances
`
786
781
`` is the class (call it :class:C
) of the instance for which the attribute
``
787
782
` reference was initiated or one of its bases, it is transformed into a bound
`
788
783
`` user-defined method object whose :attr:im_class
attribute is :class:C
and
``
789
``
`` -
whose :attr:im_self
attribute is the instance. Static method and class method
``
``
784
`` +
whose :attr:__self__
attribute is the instance. Static method and class method
``
790
785
` objects are also transformed, as if they had been retrieved from class
`
791
786
`` :class:C
; see above under "Classes". See section :ref:descriptors
for
``
792
787
` another way in which attributes of a class retrieved via its instances may
`