diff options
author | Neal Norwitz <nnorwitz@gmail.com> | 2006-08-21 17:06:07 (GMT) |
---|---|---|
committer | Neal Norwitz <nnorwitz@gmail.com> | 2006-08-21 17:06:07 (GMT) |
commit | 4886cc331ff158f8ede74878a436adfad205bd2d (patch) | |
tree | e9473cb0fd8449b2bdfcea9826e5c795e6ba87e2 /Doc/ref | |
parent | 79212998a8d46712edcf7c4f3fbaefca05a7b08b (diff) | |
download | cpython-4886cc331ff158f8ede74878a436adfad205bd2d.zip cpython-4886cc331ff158f8ede74878a436adfad205bd2d.tar.gz cpython-4886cc331ff158f8ede74878a436adfad205bd2d.tar.bz2 |
Get rid of most of the rest of coerce (slot is still there for now).
Diffstat (limited to 'Doc/ref')
-rw-r--r-- | Doc/ref/ref3.tex | 127 |
1 files changed, 2 insertions, 125 deletions
diff --git a/Doc/ref/ref3.tex b/Doc/ref/ref3.tex index 15fc188..f53dbe3 100644 --- a/Doc/ref/ref3.tex +++ b/Doc/ref/ref3.tex @@ -1645,7 +1645,7 @@ sequence types should implement addition (meaning concatenation) and multiplication (meaning repetition) by defining the methods \method{__add__()}, \method{__radd__()}, \method{__iadd__()}, \method{__mul__()}, \method{__rmul__()} and \method{__imul__()} described -below; they should not define \method{__coerce__()} or other numerical +below; they should not define other numerical operators. It is recommended that both mappings and sequences implement the \method{__contains__()} method to allow efficient use of the \code{in} operator; for mappings, \code{in} should be equivalent @@ -1689,7 +1689,7 @@ through the values. \ttindex{__imul__()} \ttindex{__contains__()} \ttindex{__iter__()}} -\withsubitem{(numeric object method)}{\ttindex{__coerce__()}} +\withsubitem{(numeric object method)} \begin{methoddesc}[container object]{__len__}{self} Called to implement the built-in function @@ -2012,129 +2012,6 @@ integer (int or long). \versionadded{2.5} \end{methoddesc} -\begin{methoddesc}[numeric object]{__coerce__}{self, other} -Called to implement ``mixed-mode'' numeric arithmetic. Should either -return a 2-tuple containing \var{self} and \var{other} converted to -a common numeric type, or \code{None} if conversion is impossible. When -the common type would be the type of \code{other}, it is sufficient to -return \code{None}, since the interpreter will also ask the other -object to attempt a coercion (but sometimes, if the implementation of -the other type cannot be changed, it is useful to do the conversion to -the other type here). A return value of \code{NotImplemented} is -equivalent to returning \code{None}. -\end{methoddesc} - -\subsection{Coercion rules\label{coercion-rules}} - -This section used to document the rules for coercion. As the language -has evolved, the coercion rules have become hard to document -precisely; documenting what one version of one particular -implementation does is undesirable. Instead, here are some informal -guidelines regarding coercion. In Python 3.0, coercion will not be -supported. - -\begin{itemize} - -\item - -If the left operand of a \% operator is a string or Unicode object, no -coercion takes place and the string formatting operation is invoked -instead. - -\item - -It is no longer recommended to define a coercion operation. -Mixed-mode operations on types that don't define coercion pass the -original arguments to the operation. - -\item - -New-style classes (those derived from \class{object}) never invoke the -\method{__coerce__()} method in response to a binary operator; the only -time \method{__coerce__()} is invoked is when the built-in function -\function{coerce()} is called. - -\item - -For most intents and purposes, an operator that returns -\code{NotImplemented} is treated the same as one that is not -implemented at all. - -\item - -Below, \method{__op__()} and \method{__rop__()} are used to signify -the generic method names corresponding to an operator; -\method{__iop__()} is used for the corresponding in-place operator. For -example, for the operator `\code{+}', \method{__add__()} and -\method{__radd__()} are used for the left and right variant of the -binary operator, and \method{__iadd__()} for the in-place variant. - -\item - -For objects \var{x} and \var{y}, first \code{\var{x}.__op__(\var{y})} -is tried. If this is not implemented or returns \code{NotImplemented}, -\code{\var{y}.__rop__(\var{x})} is tried. If this is also not -implemented or returns \code{NotImplemented}, a \exception{TypeError} -exception is raised. But see the following exception: - -\item - -Exception to the previous item: if the left operand is an instance of -a built-in type or a new-style class, and the right operand is an instance -of a proper subclass of that type or class and overrides the base's -\method{__rop__()} method, the right operand's \method{__rop__()} method -is tried \emph{before} the left operand's \method{__op__()} method. - -This is done so that a subclass can completely override binary operators. -Otherwise, the left operand's \method{__op__()} method would always -accept the right operand: when an instance of a given class is expected, -an instance of a subclass of that class is always acceptable. - -\item - -When either operand type defines a coercion, this coercion is called -before that type's \method{__op__()} or \method{__rop__()} method is -called, but no sooner. If the coercion returns an object of a -different type for the operand whose coercion is invoked, part of the -process is redone using the new object. - -\item - -When an in-place operator (like `\code{+=}') is used, if the left -operand implements \method{__iop__()}, it is invoked without any -coercion. When the operation falls back to \method{__op__()} and/or -\method{__rop__()}, the normal coercion rules apply. - -\item - -In \var{x}\code{+}\var{y}, if \var{x} is a sequence that implements -sequence concatenation, sequence concatenation is invoked. - -\item - -In \var{x}\code{*}\var{y}, if one operator is a sequence that -implements sequence repetition, and the other is an integer -(\class{int} or \class{long}), sequence repetition is invoked. - -\item - -Rich comparisons (implemented by methods \method{__eq__()} and so on) -never use coercion. Three-way comparison (implemented by -\method{__cmp__()}) does use coercion under the same conditions as -other binary operations use it. - -\item - -In the current implementation, the built-in numeric types \class{int}, -\class{long} and \class{float} do not use coercion; the type -\class{complex} however does use it. The difference can become -apparent when subclassing these types. Over time, the type -\class{complex} may be fixed to avoid coercion. All these types -implement a \method{__coerce__()} method, for use by the built-in -\function{coerce()} function. - -\end{itemize} - \subsection{With Statement Context Managers\label{context-managers}} \versionadded{2.5} |