Why do operators and methods of built-in types differ

Csaba Hoch csaba.hoch at gmail.com
Sat Jan 31 07:49:03 EST 2009


Gabriel Genellina wrote:
> The operator "+" does more than blindy calling left.__add__(right). In 
> this case, as int + list returns NotImplemented, it reverses the 
> operands and tries right.__radd__(left), and only then it gives up and 
> raises TypeError.
> 
> The actual rules are a bit more complex, involving type conversion too; 
> see http://docs.python.org/reference/datamodel.html#emulating-numeric-types

Thanks for both of you, it's much clearer now.

Just a correction: according to the doc, NotImplemented is not an
error, but a returned value.

Csaba



More information about the Python-list mailing list