[Tutor] Why x+=y instead of x=x+y?

Abel Daniel abli@freemail.hu
Fri, 19 Jul 2002 14:09:56 +0200


Scot W. Stevenson (scot@possum.in-berlin.de) wrote:
> 
> Hello Yigal, 
> 
> While reading your post on "Loopy Questions" I noticed these lines:
> 
> > 	x += 0.01
> >     x += step
> 
> Now I know this is shorthand for "x = x+0.01" and "x = x+step", but I have 
> always wondered what the point is - surely it is not too much to ask for 
> to type that extra character, especially because the resulting version is 
> not clear to newbies (why put the "+" _before_ the "=", for starters - "x 
> =+ step" would make more sense). If we want to save space, then "cnt" 
> instead of "continue" or "prt" instead of "print" might be a better place 
> to start.  
> 
> Or, to put it differently: Is this just another example of a construct that 
> was included in Python to make the "Waaah, I wanna keep my C style, even 
> if it is bizarre and non-intuitive"-crowd happy, or is there a real 
> difference between "x +=y" and "x = x+y" that justifies a notation that 
> looks like a typo?
It is really hand if you have to do somethin like this:

really['long'].data[structure].to_increment += 1

in this case if += wouldnt exist, you would have to write the data
structure two times, which not only means a lot of typing, but more
importantly, you would also have to check _every_ time that you did it
correctly and the same stuff is on both sides of the equation.

abli
abli@freemail.hu