__coerce__(self, other)]
Called to implement ``mixed-mode'' numeric arithmetic. Should either
return a 2-tuple containing self and other converted to
a common numeric type, or None if conversion is possible. When
the common type would be the type of other, it is sufficient to
return 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).
Coercion rules: to evaluate x op y, the
following steps are taken (where __op__() and
__rop__() are the method names corresponding to op,
e.g., if varop is `+', __add__() and
__radd__() are used). If an exception occurs at any point,
the evaluation is abandoned and exception handling takes over.
- 0.
- If x is a string object and op is the modulo operator (%),
the string formatting operation is invoked and the remaining steps are
skipped.
- 1.
- If x is a class instance:
- 1a.
- If x has a __coerce__() method:
replace x and y with the 2-tuple returned by
x.__coerce__(y); skip to step 2 if the
coercion returns None.
- 1b.
- If neither x nor y is a class instance
after coercion, go to step 3.
- 1c.
- If x has a method __op__(), return
x.__op__(y); otherwise, restore x and
y to their value before step 1a.
- 2.
- If y is a class instance:
- 2a.
- If y has a __coerce__() method:
replace y and x with the 2-tuple returned by
y.__coerce__(x); skip to step 3 if the
coercion returns None.
- 2b.
- If neither x nor y is a class instance
after coercion, go to step 3.
- 2b.
- If y has a method __rop__(), return
y.__rop__(x); otherwise, restore x
and y to their value before step 2a.
- 3.
- We only get here if neither x nor y is a class
instance.
- 3a.
- If op is `+' and x is a sequence,
sequence concatenation is invoked.
- 3b.
- If op is `*' and one operand is a sequence
and the other an integer, sequence repetition is invoked.
- 3c.
- Otherwise, both operands must be numbers; they are
coerced to a common type if possible, and the numeric
operation is invoked for that type.