@@ -499,22 +499,41 @@ \section{The standard type hierarchy\label{types}}
499499Methods also support accessing (but not setting) the arbitrary
500500function attributes on the underlying function object.
501501
502- User-defined method objects are created in two ways: when getting an
503- attribute of a class that is a user-defined function object, or when
504- getting an attribute of a class instance that is a user-defined
505- function object defined by the class of the instance. In the former
506- case (class attribute), the \member {im_self} attribute is \code {None},
507- and the method object is said to be unbound; in the latter case
508- (instance attribute), \method {im_self} is the instance, and the method
509- object is said to be bound. For
510- instance, when \class {C} is a class which has a method
511- \method {f()}, \code {C.f} does not yield the function object
512- \code {f}; rather, it yields an unbound method object \code {m} where
513- \code {m.im_class} is \class {C}, \code {m.im_func} is \method {f()}, and
514- \code {m.im_self} is \code {None}. When \code {x} is a \class {C}
515- instance, \code {x.f} yields a bound method object \code {m} where
516- \code {m.im_class} is \code {C}, \code {m.im_func} is \method {f()}, and
517- \code {m.im_self} is \code {x}.
502+ User-defined method objects may be created when getting an attribute
503+ of a class (perhaps via an instance of that class), if that attribute
504+ is a user-defined function object, an unbound user-defined method object,
505+ or a class method object.
506+ When the attribute is a user-defined method object, a new
507+ method object is only created if the class from which it is being
508+ retrieved is the same as, or a derived class of, the class stored
509+ in the original method object; otherwise, the original method object
510+ is used as it is.
511+
512+ When a user-defined method object is created by retrieving
513+ a user-defined function object from a class, its \member {im_self}
514+ attribute is \code {None} and the method object is said to be unbound.
515+ When one is created by retrieving a user-defined function object
516+ from a class via one of its instances, its \member {im_self} attribute
517+ is the instance, and the method object is said to be bound.
518+ In either case, the new method's \member {im_class} attribute
519+ is the class from which the retrieval takes place, and
520+ its \member {im_func} attribute is the original function object.
521+ \withsubitem {(method attribute)}{
522+ \ttindex {im_class}\ttindex {im_func}\ttindex {im_self}}
523+
524+ When a user-defined method object is created by retrieving another
525+ method object from a class or instance, the behaviour is the same
526+ as for a function object, except that the \member {im_func} attribute
527+ of the new instance is not the original method object but its
528+ \member {im_func} attribute.
529+ \withsubitem {(method attribute)}{
530+ \ttindex {im_func}}
531+
532+ When a user-defined method object is created by retrieving a
533+ class method object from a class or instance, its \member {im_self}
534+ attribute is the class itself (the same as the \member {im_class}
535+ attribute), and its \member {im_func} attribute is the function
536+ object underlying the class method.
518537\withsubitem {(method attribute)}{
519538 \ttindex {im_class}\ttindex {im_func}\ttindex {im_self}}
520539
@@ -530,6 +549,12 @@ \section{The standard type hierarchy\label{types}}
530549\method {f()}, and \code {x} is an instance of \class {C}, calling
531550\code {x.f(1)} is equivalent to calling \code {C.f(x, 1)}.
532551
552+ When a user-defined method object is derived from a class method object,
553+ the `` class instance'' stored in \member {im_self} will actually be the
554+ class itself, so that calling either \code {x.f(1)} or \code {C.f(1)} is
555+ equivalent to calling \code {f(C,1)} where \code {f} is the underlying
556+ function.
557+
533558Note that the transformation from function object to (unbound or
534559bound) method object happens each time the attribute is retrieved from
535560the class or instance. In some cases, a fruitful optimization is to
@@ -654,10 +679,19 @@ \section{The standard type hierarchy\label{types}}
654679there, the attribute search continues in the base classes. The search
655680is depth-first, left-to-right in the order of occurrence in the
656681base class list.
657- When a class attribute reference would yield a user-defined function
658- object, it is transformed into an unbound user-defined method object
659- (see above). The \member {im_class} attribute of this method object is the
660- class for which the attribute reference was initiated.
682+
683+ When a class attribute reference (for class \class {C}, say)
684+ would yield a user-defined function object or
685+ an unbound user-defined method object whose associated class is either
686+ \class {C} or one of its base classes, it is transformed into an unbound
687+ user-defined method object whose \member {im_class} attribute is~\class {C}.
688+ When it would yield a class method object, it is transformed into
689+ a bound user-defined method object whose \member {im_class} and
690+ \member {im_self} attributes are both~\class {C}. When it would yield
691+ a static method object, it is transformed into the object wrapped
692+ by the static method object. See section~\ref {descriptors } for another
693+ way in which attributes retrieved from a class may differ from those
694+ actually contained in its \member {__dict__}.
661695\obindex {class}
662696\obindex {class instance}
663697\obindex {instance}
@@ -695,11 +729,18 @@ \section{The standard type hierarchy\label{types}}
695729attribute references are searched. When an attribute is not found
696730there, and the instance's class has an attribute by that name,
697731the search continues with the class attributes. If a class attribute
698- is found that is a user-defined function object (and in no other
699- case), it is transformed into an unbound user-defined method object
700- (see above). The \member {im_class} attribute of this method object is
701- the
702- class of the instance for which the attribute reference was initiated.
732+ is found that is a user-defined function object or an unbound
733+ user-defined method object whose associated class is the class
734+ (call it~\class {C}) of the instance for which the attribute reference
735+ was initiated or one of its bases,
736+ it is transformed into a bound user-defined method object whose
737+ \member {im_class} attribute is~\class {C} whose \member {im_self} attribute
738+ is the instance. Static method and class method objects are also
739+ transformed, as if they had been retrieved from class~\class {C};
740+ see above under `` Classes'' . See section~\ref {descriptors } for
741+ another way in which attributes of a class retrieved via its
742+ instances may differ from the objects actually stored in the
743+ class's \member {__dict__}.
703744If no class attribute is found, and the object's class has a
704745\method {__getattr__()} method, that is called to satisfy the lookup.
705746\obindex {class instance}
@@ -938,6 +979,25 @@ \section{The standard type hierarchy\label{types}}
938979\versionadded {2.3}
939980\end {methoddesc }
940981
982+ \item [Static method objects]
983+ Static method objects provide a way of defeating the transformation
984+ of function objects to method objects described above. A static method
985+ object is a wrapper around any other object, usually a user-defined
986+ method object. When a static method object is retrieved from a class
987+ or a class instance, the object actually returned is the wrapped object,
988+ which is not subject to any further transformation. Static method
989+ objects are not themselves callable, although the objects they
990+ wrap usually are. Static method objects are created by the built-in
991+ \function {staticmethod()} constructor.
992+
993+ \item [Class method objects]
994+ A class method object, like a static method object, is a wrapper
995+ around another object that alters the way in which that object
996+ is retrieved from classes and class instances. The behaviour of
997+ class method objects upon such retrieval is described above,
998+ under `` User-defined methods'' . Class method objects are created
999+ by the built-in \function {classmethod()} constructor.
1000+
9411001\end {description } % Internal types
9421002
9431003\end {description } % Types
0 commit comments