@@ -1645,7 +1645,7 @@ \subsection{Emulating container types\label{sequence-types}}
16451645multiplication (meaning repetition) by defining the methods
16461646\method {__add__()}, \method {__radd__()}, \method {__iadd__()},
16471647\method {__mul__()}, \method {__rmul__()} and \method {__imul__()} described
1648- below; they should not define \method {__coerce__()} or other numerical
1648+ below; they should not define other numerical
16491649operators. It is recommended that both mappings and sequences
16501650implement the \method {__contains__()} method to allow efficient use of
16511651the \code {in} operator; for mappings, \code {in} should be equivalent
@@ -1689,7 +1689,7 @@ \subsection{Emulating container types\label{sequence-types}}
16891689 \ttindex {__imul__()}
16901690 \ttindex {__contains__()}
16911691 \ttindex {__iter__()}}
1692- \withsubitem {(numeric object method)}{ \ttindex {__coerce__()}}
1692+ \withsubitem {(numeric object method)}
16931693
16941694\begin {methoddesc }[container object]{__len__}{self}
16951695Called to implement the built-in function
@@ -2012,129 +2012,6 @@ \subsection{Emulating numeric types\label{numeric-types}}
20122012\versionadded {2.5}
20132013\end {methoddesc }
20142014
2015- \begin {methoddesc }[numeric object]{__coerce__}{self, other}
2016- Called to implement `` mixed-mode'' numeric arithmetic. Should either
2017- return a 2-tuple containing \var {self} and \var {other} converted to
2018- a common numeric type, or \code {None} if conversion is impossible. When
2019- the common type would be the type of \code {other}, it is sufficient to
2020- return \code {None}, since the interpreter will also ask the other
2021- object to attempt a coercion (but sometimes, if the implementation of
2022- the other type cannot be changed, it is useful to do the conversion to
2023- the other type here). A return value of \code {NotImplemented} is
2024- equivalent to returning \code {None}.
2025- \end {methoddesc }
2026-
2027- \subsection {Coercion rules\label {coercion-rules } }
2028-
2029- This section used to document the rules for coercion. As the language
2030- has evolved, the coercion rules have become hard to document
2031- precisely; documenting what one version of one particular
2032- implementation does is undesirable. Instead, here are some informal
2033- guidelines regarding coercion. In Python 3.0, coercion will not be
2034- supported.
2035-
2036- \begin {itemize }
2037-
2038- \item
2039-
2040- If the left operand of a \% operator is a string or Unicode object, no
2041- coercion takes place and the string formatting operation is invoked
2042- instead.
2043-
2044- \item
2045-
2046- It is no longer recommended to define a coercion operation.
2047- Mixed-mode operations on types that don't define coercion pass the
2048- original arguments to the operation.
2049-
2050- \item
2051-
2052- New-style classes (those derived from \class {object}) never invoke the
2053- \method {__coerce__()} method in response to a binary operator; the only
2054- time \method {__coerce__()} is invoked is when the built-in function
2055- \function {coerce()} is called.
2056-
2057- \item
2058-
2059- For most intents and purposes, an operator that returns
2060- \code {NotImplemented} is treated the same as one that is not
2061- implemented at all.
2062-
2063- \item
2064-
2065- Below, \method {__op__()} and \method {__rop__()} are used to signify
2066- the generic method names corresponding to an operator;
2067- \method {__iop__()} is used for the corresponding in-place operator. For
2068- example, for the operator `\code {+}', \method {__add__()} and
2069- \method {__radd__()} are used for the left and right variant of the
2070- binary operator, and \method {__iadd__()} for the in-place variant.
2071-
2072- \item
2073-
2074- For objects \var {x} and \var {y}, first \code {\var {x}.__op__(\var {y})}
2075- is tried. If this is not implemented or returns \code {NotImplemented},
2076- \code {\var {y}.__rop__(\var {x})} is tried. If this is also not
2077- implemented or returns \code {NotImplemented}, a \exception {TypeError}
2078- exception is raised. But see the following exception:
2079-
2080- \item
2081-
2082- Exception to the previous item: if the left operand is an instance of
2083- a built-in type or a new-style class, and the right operand is an instance
2084- of a proper subclass of that type or class and overrides the base's
2085- \method {__rop__()} method, the right operand's \method {__rop__()} method
2086- is tried \emph {before } the left operand's \method {__op__()} method.
2087-
2088- This is done so that a subclass can completely override binary operators.
2089- Otherwise, the left operand's \method {__op__()} method would always
2090- accept the right operand: when an instance of a given class is expected,
2091- an instance of a subclass of that class is always acceptable.
2092-
2093- \item
2094-
2095- When either operand type defines a coercion, this coercion is called
2096- before that type's \method {__op__()} or \method {__rop__()} method is
2097- called, but no sooner. If the coercion returns an object of a
2098- different type for the operand whose coercion is invoked, part of the
2099- process is redone using the new object.
2100-
2101- \item
2102-
2103- When an in-place operator (like `\code {+=}') is used, if the left
2104- operand implements \method {__iop__()}, it is invoked without any
2105- coercion. When the operation falls back to \method {__op__()} and/or
2106- \method {__rop__()}, the normal coercion rules apply.
2107-
2108- \item
2109-
2110- In \var {x}\code {+}\var {y}, if \var {x} is a sequence that implements
2111- sequence concatenation, sequence concatenation is invoked.
2112-
2113- \item
2114-
2115- In \var {x}\code {*}\var {y}, if one operator is a sequence that
2116- implements sequence repetition, and the other is an integer
2117- (\class {int} or \class {long}), sequence repetition is invoked.
2118-
2119- \item
2120-
2121- Rich comparisons (implemented by methods \method {__eq__()} and so on)
2122- never use coercion. Three-way comparison (implemented by
2123- \method {__cmp__()}) does use coercion under the same conditions as
2124- other binary operations use it.
2125-
2126- \item
2127-
2128- In the current implementation, the built-in numeric types \class {int},
2129- \class {long} and \class {float} do not use coercion; the type
2130- \class {complex} however does use it. The difference can become
2131- apparent when subclassing these types. Over time, the type
2132- \class {complex} may be fixed to avoid coercion. All these types
2133- implement a \method {__coerce__()} method, for use by the built-in
2134- \function {coerce()} function.
2135-
2136- \end {itemize }
2137-
21382015\subsection {With Statement Context Managers\label {context-managers } }
21392016
21402017\versionadded {2.5}
0 commit comments