Augument assignment versus regular assignment

Piet van Oostrum piet at cs.uu.nl
Mon Jul 17 08:05:55 EDT 2006


>>>>> Antoon Pardon <apardon at forel.vub.ac.be> (AP) wrote:

>AP> On 2006-07-14, Piet van Oostrum <piet at cs.uu.nl> wrote:

>>> Just read what it says. `It is only evaluated once' is quite clear I would
>>> say.

>AP> If it is so clear, why don't you explain it? 

I have done that in another thread.

>>> Your problem is that you thought __setitem__ is part of evaluation,

>AP> No I think the other way around. The evaluation is part of __setitem__ 
>AP> In so far as it makes sense to talk about the evaluation of a target
>AP> in python

How can evaluation be part of __setitem__? (If we talk about targets). The
LRM says clearly that it is part of assignment. 

>>> but it isn't. It is part of assignment,

>AP> No the assignment is part of __setitem__

__setitem__(  	self, key, value)
    Called to implement assignment to self[key]

>>> while __getitem__ is part of evaluation.

>AP> How can __getitem__ be part of the evaluation of col['t'] in
>AP> a statment like

>AP>   col['t'] = 5

It isn't, because there is a difference between evaluation in the
lefthandside and in the righthandside. See the description of assignment.

>AP> The language reference about augmented assigments is not only
>AP> talking about evaluating a language element as an expression,
>AP> it is also talking about evaluating that language element as
>AP> a target. Now I'm not so sure that it makes sense to talk about
>AP> evaluating a target in python, but I see no way to avoid that
>AP> interpretation in:

In the description of assignment the distinction is made for each case of
the lefthandside. So that for example when the lefthandside is a
subscription, only the primary and the index are evaluated, but no
__getitem__ is called.

>AP>   An augmented assignment expression like x += 1 can be rewritten as
>AP>   x = x + 1 to achieve a similar, but not exactly equal effect. In
>AP>   the augmented version, x is only evaluated once. 

>AP> x is only evaluated once in a statement like: x+= 1
>AP> This suggests x is evaluated twice in a statement like: x = x + 1
>AP> The only way to eavlaute x twice in that statement is that
>AP>   x is evaluated once as a target.
>AP> So the langauage reference is here talking about (among other things)
>AP> evaluating a target.

yes.

>AP> What the language reference should have said IMO is that in case x
>AP> is an attribute reference, index or slicing, the primary expression
>AP> will be evaluated only once, as will be the index or slice in the
>AP> two latter cases.

You should read the part about Augmented assignment statements after
carefully reading the part about assignments. Then you know already that
evaluation of the target is only about the primary and the index.
-- 
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