Is it just Syntactic Sugar ?

Thomas Palmer tjpalmer at cs.byu.edu
Tue May 30 13:18:56 EDT 2000


Bjorn Pettersen wrote:
> 
> 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 ?
> >
> he has considered a += method.  It would require adding more 'magic'
> methods however, so there is still some resistance...
> 
Not really. As syntactic sugar, it is just a rewrite.

i += 1 (or i++ for that matter) would be rewriten to
i = i + 1. Whether memory addresses were cached for
optimization is another issue, but the point is that
only '__add__' needs overwritten still. The compiler
would just rewrite the code.

- Tom



More information about the Python-list mailing list