@@ -529,92 +529,74 @@ Callable types
529529 single: __kwdefaults__ (function attribute)
530530 pair: global; namespace
531531
532- User-defined methods
532+ Instance methods
533533 .. index ::
534534 object: method
535535 object: user-defined method
536536 pair: user-defined; method
537537
538- A user-defined method object combines a class, a class instance (or ``None ``)
539- and any callable object (normally a user-defined function).
538+ An instance method object combines a class, a class instance and any
539+ callable object (normally a user-defined function).
540+
541+ .. index ::
542+ single: __func__ (method attribute)
543+ single: __self__ (method attribute)
544+ single: __doc__ (method attribute)
545+ single: __name__ (method attribute)
546+ single: __module__ (method attribute)
540547
541548 Special read-only attributes: :attr: `__self__ ` is the class instance object,
542549 :attr: `__func__ ` is the function object; :attr: `__doc__ ` is the method's
543550 documentation (same as ``__func__.__doc__ ``); :attr: `__name__ ` is the
544551 method name (same as ``__func__.__name__ ``); :attr: `__module__ ` is the
545552 name of the module the method was defined in, or ``None `` if unavailable.
546553
547- .. index ::
548- single: __doc__ (method attribute)
549- single: __name__ (method attribute)
550- single: __module__ (method attribute)
551- single: __func__ (method attribute)
552- single: __self__ (method attribute)
553-
554554 Methods also support accessing (but not setting) the arbitrary function
555555 attributes on the underlying function object.
556556
557- User-defined method objects may be created when getting an attribute of a class
558- (perhaps via an instance of that class), if that attribute is a user-defined
559- function object, an unbound user-defined method object, or a class method
560- object. When the attribute is a user-defined method object, a new method object
561- is only created if the class from which it is being retrieved is the same as, or
562- a derived class of, the class stored in the original method object; otherwise,
563- the original method object is used as it is.
564-
565- .. index ::
566- single: __func__ (method attribute)
567- single: __self__ (method attribute)
568-
569- When a user-defined method object is created by retrieving a user-defined
570- function object from a class, its :attr: `__self__ ` attribute is ``None ``
571- and the method object is said to be unbound. When one is created by
572- retrieving a user-defined function object from a class via one of its
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.
576-
577- .. index :: single: __func__ (method attribute)
578-
579- When a user-defined method object is created by retrieving another method object
580- from a class or instance, the behaviour is the same as for a function object,
581- except that the :attr: `__func__ ` attribute of the new instance is not the
582- original method object but its :attr: `__func__ ` attribute.
583-
584- .. index ::
585- single: __func__ (method attribute)
586- single: __self__ (method attribute)
587-
588- When a user-defined method object is created by retrieving a class method object
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
591- the function object underlying the class method.
592-
593- When an unbound user-defined method object is called, the underlying function
594- (:attr: `__func__ `) is called, with the restriction that the first argument must
595- be an instance of the proper class (:attr: `im_class `) or of a derived class
596- thereof.
597-
598- When a bound user-defined method object is called, the underlying function
599- (:attr: `__func__ `) is called, inserting the class instance (:attr: `__self__ `) in
600- front of the argument list. For instance, when :class: `C ` is a class which
601- contains a definition for a function :meth: `f `, and ``x `` is an instance of
602- :class: `C `, calling ``x.f(1) `` is equivalent to calling ``C.f(x, 1) ``.
603-
604- When a user-defined method object is derived from a class method object, the
605- "class instance" stored in :attr: `__self__ ` will actually be the class itself, so
606- that calling either ``x.f(1) `` or ``C.f(1) `` is equivalent to calling ``f(C,1) ``
607- where ``f `` is the underlying function.
608-
609- Note that the transformation from function object to (unbound or bound) method
610- object happens each time the attribute is retrieved from the class or instance.
611- In some cases, a fruitful optimization is to assign the attribute to a local
612- variable and call that local variable. Also notice that this transformation only
613- happens for user-defined functions; other callable objects (and all non-callable
614- objects) are retrieved without transformation. It is also important to note
615- that user-defined functions which are attributes of a class instance are not
616- converted to bound methods; this *only * happens when the function is an
617- attribute of the class.
557+ User-defined method objects may be created when getting an attribute of a
558+ class (perhaps via an instance of that class), if that attribute is a
559+ user-defined function object or a class method object.
560+
561+ When an instance method object is created by retrieving a user-defined
562+ function object from a class via one of its instances, its
563+ :attr: `__self__ ` attribute is the instance, and the method object is said
564+ to be bound. The new method's :attr: `__func__ ` attribute is the original
565+ function object.
566+
567+ When a user-defined method object is created by retrieving another method
568+ object from a class or instance, the behaviour is the same as for a
569+ function object, except that the :attr: `__func__ ` attribute of the new
570+ instance is not the original method object but its :attr: `__func__ `
571+ attribute.
572+
573+ When an instance method object is created by retrieving a class method
574+ object from a class or instance, its :attr: `__self__ ` attribute is the
575+ class itself, and its :attr: `__func__ ` attribute is the function object
576+ underlying the class method.
577+
578+ When an instance method object is called, the underlying function
579+ (:attr: `__func__ `) is called, inserting the class instance
580+ (:attr: `__self__ `) in front of the argument list. For instance, when
581+ :class: `C ` is a class which contains a definition for a function
582+ :meth: `f `, and ``x `` is an instance of :class: `C `, calling ``x.f(1) `` is
583+ equivalent to calling ``C.f(x, 1) ``.
584+
585+ When an instance method object is derived from a class method object, the
586+ "class instance" stored in :attr: `__self__ ` will actually be the class
587+ itself, so that calling either ``x.f(1) `` or ``C.f(1) `` is equivalent to
588+ calling ``f(C,1) `` where ``f `` is the underlying function.
589+
590+ Note that the transformation from function object to instance method
591+ object happens each time the attribute is retrieved from the instance. In
592+ some cases, a fruitful optimization is to assign the attribute to a local
593+ variable and call that local variable. Also notice that this
594+ transformation only happens for user-defined functions; other callable
595+ objects (and all non-callable objects) are retrieved without
596+ transformation. It is also important to note that user-defined functions
597+ which are attributes of a class instance are not converted to bound
598+ methods; this *only * happens when the function is an attribute of the
599+ class.
618600
619601 Generator functions
620602 .. index ::
@@ -731,16 +713,12 @@ Custom classes
731713 pair: class; attribute
732714
733715 When a class attribute reference (for class :class: `C `, say) would yield a
734- user-defined function object or an unbound user-defined method object whose
735- associated class is either :class: `C ` or one of its base classes, it is
736- transformed into an unbound user-defined method object whose :attr: `im_class `
737- attribute is :class: `C `. When it would yield a class method object, it is
738- transformed into a bound user-defined method object whose :attr: `im_class `
739- and :attr: `__self__ ` attributes are both :class: `C `. When it would yield a
740- static method object, it is transformed into the object wrapped by the static
741- method object. See section :ref: `descriptors ` for another way in which
742- attributes retrieved from a class may differ from those actually contained in
743- its :attr: `__dict__ `.
716+ class method object, it is transformed into an instance method object whose
717+ :attr: `__self__ ` attributes is :class: `C `. When it would yield a static
718+ method object, it is transformed into the object wrapped by the static method
719+ object. See section :ref: `descriptors ` for another way in which attributes
720+ retrieved from a class may differ from those actually contained in its
721+ :attr: `__dict__ `.
744722
745723 .. index :: triple: class; attribute; assignment
746724
@@ -772,22 +750,19 @@ Class instances
772750 pair: class; instance
773751 pair: class instance; attribute
774752
775- A class instance is created by calling a class object (see above). A class
776- instance has a namespace implemented as a dictionary which is the first place in
777- which attribute references are searched. When an attribute is not found there,
778- and the instance's class has an attribute by that name, the search continues
779- with the class attributes. If a class attribute is found that is a user-defined
780- function object or an unbound user-defined method object whose associated class
781- is the class (call it :class: `C `) of the instance for which the attribute
782- reference was initiated or one of its bases, it is transformed into a bound
783- user-defined method object whose :attr: `im_class ` attribute is :class: `C ` and
784- whose :attr: `__self__ ` attribute is the instance. Static method and class method
785- objects are also transformed, as if they had been retrieved from class
786- :class: `C `; see above under "Classes". See section :ref: `descriptors ` for
787- another way in which attributes of a class retrieved via its instances may
788- differ from the objects actually stored in the class's :attr: `__dict__ `. If no
789- class attribute is found, and the object's class has a :meth: `__getattr__ `
790- method, that is called to satisfy the lookup.
753+ A class instance is created by calling a class object (see above). A class
754+ instance has a namespace implemented as a dictionary which is the first place
755+ in which attribute references are searched. When an attribute is not found
756+ there, and the instance's class has an attribute by that name, the search
757+ continues with the class attributes. If a class attribute is found that is a
758+ user-defined function object, it is transformed into an instance method
759+ object whose :attr: `__self__ ` attribute is the instance. Static method and
760+ class method objects are also transformed; see above under "Classes". See
761+ section :ref: `descriptors ` for another way in which attributes of a class
762+ retrieved via its instances may differ from the objects actually stored in
763+ the class's :attr: `__dict__ `. If no class attribute is found, and the
764+ object's class has a :meth: `__getattr__ ` method, that is called to satisfy
765+ the lookup.
791766
792767 .. index :: triple: class instance; attribute; assignment
793768
0 commit comments