[Tutor] multiple assignment on one line

Luke Paireepinart rabidpoobear at gmail.com
Sun Nov 22 12:56:25 CET 2009


On Sun, Nov 22, 2009 at 2:30 AM, Shashwat Anand <anand.shashwat at gmail.com>wrote:

> Ok, this is silly but i want to know is multiple assignment is possible in
> one line using "+=, -=, *= etc" operators on python 2.6.4
>
> like a,b = 0,1 which is equal to
> a = 0
> b = 1
> on seperate lines.
> similarly a = b = 0 which is equal to
> a = 0
> b = 0
> on seperate lines.
>

Please note this is only the same because you happen to be using an
immutable type!
using the 'on separate lines' version is preferable because you won't
accidentally do something like this:
>>> a = b = []
>>> a.append('foo')
>>> a
['foo']
>>> b
['foo']
>>>
if you forget your type is mutable.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091122/fdfe607a/attachment.htm>


More information about the Tutor mailing list