Tuesday, April 23, 2013

Coercion rules in python[summary]

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 __rop__()
method, the right operand’s __rop__() method is tried before the left operand’s __op__() method.

When an in-place operator (like ‘+=‘) is used, if the left operand implements __iop__(), it is invoked without
any coercion(convert is explicit and coerce is implicit, for exam, 1.0+2)

Rich comparisons (implemented by methods __eq__() and so on) never use coercion. Three-way comparison
(implemented by __cmp__()) does use coercion under the same conditions as other binary operations use it.

No comments:

Post a Comment