Why do operators and methods of built-in types differ
Csaba Hoch
csaba.hoch at gmail.com
Sat Jan 31 06:51:35 EST 2009
Hi,
if I write the following:
>>> 1+1
2
it seems to be exactly equivalent to this:
>>> (1).__add__(1)
2
However, if I write invalid code and try to add a list to an int, the
errors will be different:
>>> 1+[]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'list'
>>> (1).__add__([])
NotImplemented
I found that operator.__add__(1, []) gives the same result as 1+[].
What is the reason behind this difference between the __add__ operator
and int.__add__?
Thank you,
Csaba
More information about the Python-list
mailing list