Is it just Syntactic Sugar ?
Konrad Hinsen
hinsen at cnrs-orleans.fr
Tue May 23 09:27:16 EDT 2000
"Eric Hagemann" <ehagemann at home.com> writes:
> 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 ?
Yes, but it's not quite twice the same. Your statement gets translated
into something like:
a[x][f].__setitem__(g, a[x][f].__getitem__(g).__add__(1))
(the details depend on the precise types involved, there is a difference
between types implemented in C and those implemented as classes in Pyton).
The subexpression a[x][f] is evaluated twice, and the language doesn't
even guarantee that the result is the same. The first evaluation could
modify the object 'a' as a side effect, although that would of course
be a case of extremely bad program design.
> Would the a+=1 for speed things up or is it just expanded to a=a+1 anyway ?
That depends on how a+=1 would be defined! If it were defined to be
exactly equivalent to "a = a + 1" under all possible circumstances,
then it would have to generate exactly the same code.
> I have found that
>
> p=a[x][f][g]
> p=p+1
This does something different, it doesn't modify the original sequence.
--
-------------------------------------------------------------------------------
Konrad Hinsen | E-Mail: hinsen at cnrs-orleans.fr
Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-2.38.25.55.69
Rue Charles Sadron | Fax: +33-2.38.63.15.17
45071 Orleans Cedex 2 | Deutsch/Esperanto/English/
France | Nederlands/Francais
-------------------------------------------------------------------------------
More information about the Python-list
mailing list