return type question - operator oveloading

arthur.siegel at rsmi.com arthur.siegel at rsmi.com
Sat Mar 16 22:53:22 EST 2002


But

>>> complex(4,5)*7
(28+35j)
>>> 7*complex(4,5)
(28+35j)
>>> complex(4,5)+7
(11+5j)
>>> 7+complex(4,5)
(11+5j)

And

>>> int(4).__add__(complex(3,4))
NotImplemented
>>> int(4).__mul__(complex(3,4))
NotImplemented
>>> complex(3,4).__add__(4)
NotImplemented
>>> complex(3,4).__mul__(4)
NotImplemented

Not sure what it really means at a deeper level, but it does
seem the implementation on multiply and add for complex with int *is*
fully symetrical.

still ?

Art

>>arthur.siegel at rsmi.com wrote in
>>news:mailman.1016322689.22392.python-list at python.org:
>> But:
>>
>> print type(7 + Complex(5,4))
>>><type 'complex'>

chirs-
>yes i get the same results (py 2.2).

art
>> Multiple choice:
>>   That the result at But is a surprise to me because I am missing:
>>       1)Something obvious about __radd__ or general classic syntax
>>       2)Something related to new style classes
>>       3)Other

chris -
>i vote for "3) Other". i think the integer class defines __add__ for
>complex numbers but not __mul__ for complex numbers. thus when adding
>"7+Complex(5,4)" your __radd__ is never called cause the int class
>indles it.








More information about the Python-list mailing list