Index: ref3.tex =================================================================== --- ref3.tex (revision 42105) +++ ref3.tex (working copy) @@ -594,6 +594,29 @@ not converted to bound methods; this \emph{only} happens when the function is an attribute of the class. +Because this transformation happens with every retrieval, the following code +does not work as one might naively expect: + + \begin{verbatim} +>>> my_object.some_method == my_object.some_method + True +>>> my_object.some_method is my_object.some_method + False +>>> + \end{verbatim} + +Here, the method has been retrieved twice, resulting in two different +- though equal - objects. In the next snippet, the method is retrieved only +once, so the \keyword{is} comparison functions as expected: + + \begin{verbatim} +>>> method = my_object.some_method +>>> method is method + True +>>> + \end{verbatim} +\opindex{is} + \item[Generator functions\index{generator!function}\index{generator!iterator}] A function or method which uses the \keyword{yield} statement (see section~\ref{yield}, ``The \keyword{yield} statement'') is called a @@ -1028,6 +1051,29 @@ under ``User-defined methods''. Class method objects are created by the built-in \function{classmethod()} constructor. +Note that because of this wrapper object, there is a slight caveat when using +class methods in conjunction with the \keyword{is} operator. For example, the +following code works as expected: + + \begin{verbatim} +>>> cm = MyClass.some_class_method +>>> cm is cm + True +>>> + \end{verbatim} + +while this code does not: + + \begin{verbatim} +>>> MyClass.some_class_method is MyClass.some_class_method + False +>>> + \end{verbatim} + +In the second snippet, the class method has been retrieved twice, resulting in +two different -- though equal -- objects. +\opindex{is} + \end{description} % Internal types \end{description} % Types