Augument assignment versus regular assignment

Antoon Pardon apardon at forel.vub.ac.be
Tue Jul 11 06:23:10 EDT 2006


On 2006-07-10, Terry Reedy <tjreedy at udel.edu> wrote:
>
> "Antoon Pardon" <apardon at forel.vub.ac.be> wrote in message 
> news:slrneb4l7c.rg3.apardon at rcpc42.vub.ac.be...
>> I disagree. The += version only evaluates the index once, but still has
>> to find the object twice.
>
> No it does not *have* to find the object twice and no it does *not* find 
> the object twice.  From the viewpoint of the interpreter, the purpose of 
> abbreviating 'objectexpression = objectexpression op arg' as 
> 'objectexpression op=arg' is to avoid unnecessary recalculation

But is the viewpoint of the interpreter somehow relevant? IMO the
question is if the actual behaviour is compatible with what people
expect after having read the language reference. The viewpoint of
the interpreter IMO doesn't play a role in answering that question.

>> But as far as I can interpret what is happening from the printed lines
>
> Your print get/set examples from your black-box testing miss the following 
> key point.  While the interpreter still has to *use* the object(s) twice, 
> once to get and once to set, it no longer has to *calculate* the objects 
> twice.

The language reference doesn't talk about objects. And IMO you
should be carefull if you want to use the word "object" here.
In the line: "foo += 1", you can't talk about the object foo,
since foo will be bound to a different object after the assignment
than it was bound to before.

As I read the language reference the x stands for a target expression.
Now what does it mean to evaluate a target expression like col[key].
IMO it means finding the location of the item in the collection: the
bucket in the directory, the node in the tree ... grosso mode it
boils down to the call to __setitem__ or __getitem__ depending
on where the col[key] was located in the line (or if you prefer
the view from the interpreter it boils down to the BINARY_SUBSCR
and STORE_SUBSCR opcodes).

So if the language reference seems to implies that col[key] will
only be evaluated once in a line like: "col[key] += 1" I expect
only one call from __setitem__ or __getitem__ (or only one
from BINARY_SUBSCR or STORE_SUBSCR)

Now I know python doesn't behave this way, but how python
actually behave can't be used as an argument that this
is the behaviour as described by the language reference.

So my question is: suppose I write my own collector,
where __setitem__ and __getitem__ have the same side effect.
How many times should/will this side effect occur in code like
"col[key] += 1". As I read the language reference it should
happen only once, however that is not what happens. So if
the actual behaviour of python is what we want, which is
what I suspect, then the language reference should
clarify more what the supposed behaviour should be.

Now my reading of the language reference can be faulty,
but if you want to argue that, I would appreciate it
if you could explain how I have to read the language
reference in order to come to the conclusion that the
side effect in this example has to happen twice.

And even in this case would I suggest that the language
reference would better be made clearer, since I doubt
that I'm the only who will read the language reference
this way.

-- 
Antoon Pardon



More information about the Python-list mailing list