Is it just Syntactic Sugar ?

Bjorn Pettersen bjorn at roguewave.com
Mon May 22 21:53:02 EDT 2000


Eric Hagemann wrote:
> 
> Based on some recent code I have been writing I am courious on
> describing the following as 'syntactic sugar'
> 
> a common operation is to
> 
> a=a+1
> 
> which in C can be written as
> 
> a+=1
> 
> in its simplest form I agree there might not be much to this but if you
> do this with a complex expression as
> 
> a[x][f][g] = a[x][f][g] + 1
> 
> does the python engine recompute the 'address' of the variable 'a[x][f][g]'
> twice ?
> 
> Would the a+=1 for speed things up or is it just expanded to a=a+1 anyway ?
> 
> I have found that
> 
> p=a[x][f][g]
> p=p+1
> 
> is faster than above so I kinda believe the 'address' is computed twice (I
> am leaping here)
> 
> Comments / Thoughts ?
> 
> Cheers
> Eric

Speed isn't really an issue in python, however I recently found myself
writing:

  self.stats[tot] = self.stats[tot] + bugs[k]

which was a mouthful...  Guido and others have historically been
strongly opposed to adding -- and ++ operators, but I recently read that
he has considered a += method.  It would require adding more 'magic'
methods however, so there is still some resistance...

-- bjorn

ps: yes, Python will recompute the indices since the [] operator is only
a call to __getitem__ which is free to do anything, including changing
the array.




More information about the Python-list mailing list