Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 92cf95f

Browse files
committed
Addressed SF bug 421973 (finally).
Rewrote the subsection on coercion rules (and made it a proper subsection, with a label). The new section is much less precise, because precise rules would be too hard to give (== I don't know what they are any more :-). OTOH, the new section gives much more up-to-date information. Also noted that __coerce__ may return NotImplemented, with the same meaning as None. I beg Fred forgiveness: my use of \code{} is probably naive. Please fix this and other markup nits. An index entry would be nice. This could be a 2.2 bugfix candidate, if we bother about old docs (Fred?)
1 parent 40bbae3 commit 92cf95f

1 file changed

Lines changed: 88 additions & 46 deletions

File tree

Doc/ref/ref3.tex

Lines changed: 88 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,74 +1511,116 @@ \subsection{Emulating numeric types\label{numeric-types}}
15111511
return \code{None}, since the interpreter will also ask the other
15121512
object to attempt a coercion (but sometimes, if the implementation of
15131513
the other type cannot be changed, it is useful to do the conversion to
1514-
the other type here).
1514+
the other type here). A return value of \code{NotImplemented} is
1515+
equivalent to returning \code{None}.
15151516
\end{methoddesc}
15161517

1517-
\strong{Coercion rules}: to evaluate \var{x} \var{op} \var{y}, the
1518-
following steps are taken (where \method{__\var{op}__()} and
1519-
\method{__r\var{op}__()} are the method names corresponding to
1520-
\var{op}, e.g., if \var{op} is `\code{+}', \method{__add__()} and
1521-
\method{__radd__()} are used). If an exception occurs at any point,
1522-
the evaluation is abandoned and exception handling takes over.
1518+
\subsection{Coercion rules\label{coercion-rules}}
1519+
1520+
This section used to document the rules for coercion. As the language
1521+
has evolved, the coercion rules have become hard to document
1522+
precisely; documenting what one version of one particular
1523+
implementation does is undesirable. Instead, here are some informal
1524+
guidelines regarding coercion. In Python 3.0, coercion will not be
1525+
supported.
15231526

15241527
\begin{itemize}
15251528

1526-
\item[0.] If \var{x} is a string object and \var{op} is the modulo
1527-
operator (\%), the string formatting operation is invoked and
1528-
the remaining steps are skipped.
1529+
\item
1530+
1531+
If the left operand of a \% operator is a string or Unicode object, no
1532+
coercion takes place and the string formatting operation is invoked
1533+
instead.
1534+
1535+
\item
1536+
1537+
It is no longer recommended to define a coercion operation.
1538+
Mixed-mode operations on types that don't define coercion pass the
1539+
original arguments to the operation.
1540+
1541+
\item
1542+
1543+
New-style classes (those derived from \code{object}) never invoke the
1544+
\code{__coerce__} method in response to a binary operator; the only
1545+
time \code{__coerce__} is invoked is when the built-in function
1546+
\code{coerce()} is called.
1547+
1548+
\item
1549+
1550+
For most intents and purposes, an operator that returns
1551+
\code{NotImplemented} is treated the same as one that is not
1552+
implemented at all.
15291553

1530-
\item[1.] If \var{x} is a class instance:
1554+
\item
15311555

1532-
\begin{itemize}
1556+
Below, \method{__op__()} and \method{__rop__()} are used to signify
1557+
the generic method names corresponding to an operator;
1558+
\method{__iop__} is used for the corresponding in-place operator. For
1559+
example, for the operator `\code{+}', \method{__add__()} and
1560+
\method{__radd__()} are used for the left and right variant of the
1561+
binary operator, and \method{__iadd__} for the in-place variant.
15331562

1534-
\item[1a.] If \var{x} has a \method{__coerce__()} method:
1535-
replace \var{x} and \var{y} with the 2-tuple returned by
1536-
\code{\var{x}.__coerce__(\var{y})}; skip to step 2 if the
1537-
coercion returns \code{None}.
1563+
\item
15381564

1539-
\item[1b.] If neither \var{x} nor \var{y} is a class instance
1540-
after coercion, go to step 3.
1565+
For objects \var{x} and \var{y}, first \code{\var{x}.__op__(\var{y})}
1566+
is tried. If this is not implemented or returns \code{NotImplemented},
1567+
\code{\var{y}.__rop__(\var{x})} is tried. If this is also not
1568+
implemented or returns \code{NotImplemented}, a \code{TypeError}
1569+
exception is raised. But see the following exception:
15411570

1542-
\item[1c.] If \var{x} has a method \method{__\var{op}__()}, return
1543-
\code{\var{x}.__\var{op}__(\var{y})}; otherwise, restore \var{x} and
1544-
\var{y} to their value before step 1a.
1571+
\item
15451572

1546-
\end{itemize}
1573+
Exception to the previous item: if the left operand is an instance of
1574+
a built-in type or a new-style class, and the right operand is an
1575+
instance of a proper subclass of that type or class, the right
1576+
operand's \code{__rop__} method is tried \emph{before} the left
1577+
operand's \code{__op__} method. This is done so that a subclass can
1578+
completely override binary operators. Otherwise, the left operand's
1579+
__op__ method would always accept the right operand: when an instance
1580+
of a given class is expected, an instance of a subclass of that class
1581+
is always acceptable.
15471582

1548-
\item[2.] If \var{y} is a class instance:
1583+
\item
15491584

1550-
\begin{itemize}
1585+
When either operand type defines a coercion, this coercion is called
1586+
before that type's \code{__op__} or \code{__rop__} method is called,
1587+
but no sooner. If the coercion returns an object of a different type
1588+
for the operand whose coercion is invoked, part of the process is
1589+
redone using the new object.
15511590

1552-
\item[2a.] If \var{y} has a \method{__coerce__()} method:
1553-
replace \var{y} and \var{x} with the 2-tuple returned by
1554-
\code{\var{y}.__coerce__(\var{x})}; skip to step 3 if the
1555-
coercion returns \code{None}.
1591+
\item
15561592

1557-
\item[2b.] If neither \var{x} nor \var{y} is a class instance
1558-
after coercion, go to step 3.
1593+
When an in-place operator (like `\code{+=}') is used, if the left
1594+
operand implements \code{__iop__}, it is invoked without any coercion.
1595+
When the operation falls back to \code{__op__} and/or \code{__rop__},
1596+
the normal coercion rules apply.
15591597

1560-
\item[2b.] If \var{y} has a method \method{__r\var{op}__()},
1561-
return \code{\var{y}.__r\var{op}__(\var{x})}; otherwise,
1562-
restore \var{x} and \var{y} to their value before step 2a.
1598+
\item
15631599

1564-
\end{itemize}
1600+
In \var{x}\code{+}\var{y}, if \var{x} is a sequence that implements
1601+
sequence concatenation, sequence concatenation is invoked.
15651602

1566-
\item[3.] We only get here if neither \var{x} nor \var{y} is a class
1567-
instance.
1603+
\item
15681604

1569-
\begin{itemize}
1605+
In \var{x}\code{*}\var{y}, if one operator is a sequence that
1606+
implements sequence repetition, and the other is an integer
1607+
(\code{int} or \code{long}), sequence repetition is invoked.
15701608

1571-
\item[3a.] If \var{op} is `\code{+}' and \var{x} is a
1572-
sequence, sequence concatenation is invoked.
1609+
\item
15731610

1574-
\item[3b.] If \var{op} is `\code{*}' and one operand is a
1575-
sequence and the other an integer, sequence repetition is
1576-
invoked.
1611+
Rich comparisons (implemented by methods \code{__eq__} and so on)
1612+
never use coercion. Three-way comparison (implemented by
1613+
\code{__cmp__}) does use coercion under the same conditions as
1614+
other binary operations use it.
15771615

1578-
\item[3c.] Otherwise, both operands must be numbers; they are
1579-
coerced to a common type if possible, and the numeric
1580-
operation is invoked for that type.
1616+
\item
15811617

1582-
\end{itemize}
1618+
In the current implementation, the built-in numeric types \code{int},
1619+
\code{long} and \code{float} do not use coercion; the type
1620+
\code{complex} however does use it. The difference can become
1621+
apparent when subclassing these types. Over time, the type
1622+
\code{complex} may be fixed to avoid coercion. All these types
1623+
implement a \code{__coerce__} method, for use by the built-in
1624+
\code{coerce} function.
15831625

15841626
\end{itemize}

0 commit comments

Comments
 (0)