Augument assignment versus regular assignment

Piet van Oostrum piet at cs.uu.nl
Sat Jul 15 05:14:02 EDT 2006


>>>>> Gerhard Fiedler <gelists at gmail.com> (GF) wrote:

>GF> On 2006-07-14 16:07:28, Piet van Oostrum wrote:
>AP> 2a) In case you answer yes to question (1). Can you explain me how
>AP> I have to read the language reference in order to deduce this
>AP> is indeed the way things should be understood.
>>> 
>>> Just read what it says. `It is only evaluated once' is quite clear I would
>>> say. Your problem is that you thought __setitem__ is part of evaluation,
>>> but it isn't. It is part of assignment, while __getitem__ is part of
>>> evaluation. See the definitions of __getitem__ and __setitem__ in the
>>> language reference manual.

>GF> Sorry to butt in here... I really don't know much more about this than I
>GF> read in this thread :) 

>GF> But wasn't stated earlier that one of the differences between a += b and a
>GF> = a + b is that a gets evaluated once in the first case and twice in the
>GF> second case? If __getitem__ was part of the evaluation (as you seem to
>GF> say), shouldn't it be called twice in the second case? It doesn't seem to
>GF> get called twice; see this snippet from an earlier message:

>>>>> t['a'] = t['a'] + 1
>GF> __getitem__, key = a
>GF> __setitem__, key = a
>>>>> t['a'] += 1
>GF> __getitem__, key = a
>GF> __setitem__, key = a


>GF> Seems like the __get/setitem__ thing has not much to do with what the
>GF> manual calls evaluation, but rather with what the name implies: setting and
>GF> getting the value of the item. And therefore, since in both the a += b case
>GF> and the a = a + b case the value of a is gotten once and set once,
>GF> __getitem__ gets called once and __setitem__ gets called once. No?

Yes, in both cases you get the value once, and you set the value once.

In an assignment, the lefthand side is evaluated differently from the
righthand side, of course. Because in the righthand side you need the value
of the object, but in the lefthand side you need only the 'location' (this
is not a Python term).
Therefore in the righthand side __getitem__ is part of the evaluation.
In the lefthand side when it is a[i], only a and i are evaluated, but then
the evaluation stops. Next the assignment is done with __setitem__.

Now if it is an augmented assignment like a[i]+=b, It is only evaluated
once. But we need it both as a lefthand side and a righthand side. So this
means that a and i are evaluated (but only once!). For the lefthand side
this completes the evaluation. And then the results of these are used as
parameters to __getitem__ to complete the evaluation of the righthand side.
Assuming b had already been evaluated, next the assignment is performed
by calling __setitem__.

Your example above doesn't show any difference because t['a'] doesn't have
any side effects. But if you use for both t and the index a function that
prints something you will see the difference.
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list