Commit 35c09f2e authored by Fred Drake's avatar Fred Drake

Revise the description of when functions retrieved from class instances

are and are not turned into bound methods; some confusion was noted by
Andrew Dalke.

In particular, it has to be noted that functions located on the class
instance are not turned into any sort of method, only those which are
found via the underlying class.
parent 6da0b914
...@@ -438,11 +438,12 @@ base class of the class of which \member{im_self} is an instance); ...@@ -438,11 +438,12 @@ base class of the class of which \member{im_self} is an instance);
User-defined method objects are created in two ways: when getting an User-defined method objects are created in two ways: when getting an
attribute of a class that is a user-defined function object, or when attribute of a class that is a user-defined function object, or when
getting an attributes of a class instance that is a user-defined getting an attribute of a class instance that is a user-defined
function object. In the former case (class attribute), the function object defined by the class of the instance. In the former
\member{im_self} attribute is \code{None}, and the method object is said case (class attribute), the \member{im_self} attribute is \code{None},
to be unbound; in the latter case (instance attribute), \method{im_self} and the method object is said to be unbound; in the latter case
is the instance, and the method object is said to be bound. For (instance attribute), \method{im_self} is the instance, and the method
object is said to be bound. For
instance, when \class{C} is a class which contains a definition for a instance, when \class{C} is a class which contains a definition for a
function \method{f()}, \code{C.f} does not yield the function object function \method{f()}, \code{C.f} does not yield the function object
\code{f}; rather, it yields an unbound method object \code{m} where \code{f}; rather, it yields an unbound method object \code{m} where
...@@ -452,9 +453,7 @@ instance, \code{x.f} yields a bound method object \code{m} where ...@@ -452,9 +453,7 @@ instance, \code{x.f} yields a bound method object \code{m} where
\code{m.im_class} is \code{C}, \code{m.im_func} is \method{f()}, and \code{m.im_class} is \code{C}, \code{m.im_func} is \method{f()}, and
\code{m.im_self} is \code{x}. \code{m.im_self} is \code{x}.
\withsubitem{(method attribute)}{ \withsubitem{(method attribute)}{
\ttindex{im_class} \ttindex{im_class}\ttindex{im_func}\ttindex{im_self}}
\ttindex{im_func}
\ttindex{im_self}}
When an unbound user-defined method object is called, the underlying When an unbound user-defined method object is called, the underlying
function (\member{im_func}) is called, with the restriction that the function (\member{im_func}) is called, with the restriction that the
...@@ -474,7 +473,10 @@ the class or instance. In some cases, a fruitful optimization is to ...@@ -474,7 +473,10 @@ the class or instance. In some cases, a fruitful optimization is to
assign the attribute to a local variable and call that local variable. assign the attribute to a local variable and call that local variable.
Also notice that this transformation only happens for user-defined Also notice that this transformation only happens for user-defined
functions; other callable objects (and all non-callable objects) are functions; other callable objects (and all non-callable objects) are
retrieved without transformation. retrieved without transformation. It is also important to note that
user-defined functions which are attributes of a class instance are
not converted to bound methods; this \emph{only} happens when the
function is an attribute of the class.
\item[Built-in functions] \item[Built-in functions]
A built-in function object is a wrapper around a \C{} function. Examples A built-in function object is a wrapper around a \C{} function. Examples
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment