special operator =+

Murtog (sent by Nabble.com) lists at nabble.com
Fri Dec 16 23:54:48 EST 2005


This isnt related to any operator. It is just a assigment operator with a plus operator. This can be used with number without raising any error:

In [12]: a =+ 5
In [13]: a
Out[13]: 5
In [14]: a =+ -5
In [15]: a
Out[15]: -5
In [16]: a =+ 5.8
In [17]: a
Out[17]: 5.7999999999999998
In [18]: a =+ -5.8
In [19]: a
Out[19]: -5.7999999999999998

But if you use with any other type it will raise an error:

In [40]: a =+ 'test'
---------------------------------------------------------------------------
exceptions.TypeError                                 Traceback (most recent call last)

/home/murtog/ 

TypeError: bad operand type for unary +

In [41]: a =+ []
---------------------------------------------------------------------------
exceptions.TypeError                                 Traceback (most recent call last)

/home/murtog/ 

TypeError: bad operand type for unary +

In [42]: a =+ {}
---------------------------------------------------------------------------
exceptions.TypeError                                 Traceback (most recent call last)

/home/murtog/ 

TypeError: bad operand type for unary +


Ok ? i think that you wanted the: __add__ method. this other sample may help you:

In [66]: a = 5

In [67]: a.__add__(3)
Out[67]: 8

In [68]: a
Out[68]: 5

In [69]: a = a.__add__(3)
In [70]: a
Out[70]: 8

I hope this help you. I havent found the __iadd__ operator... 



--
Sent from the Python - python-list forum at Nabble.com:
http://www.nabble.com/special-operator-%3D%2B-t751771.html#a1984726
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20051216/bbc47071/attachment.html>


More information about the Python-list mailing list