Why do operators and methods of built-in types differ
Scott David Daniels
Scott.Daniels at Acm.Org
Sat Jan 31 12:44:13 EST 2009
Csaba Hoch wrote:
> 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: ....
As has been explained, binary operators are trickier than the above
seems to show. Here is how to get the full behavior:
>>> import operator
>>> operator.add(1,[])
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
operator.add(1,[])
TypeError: unsupported operand type(s) for +: 'int' and 'list'
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list