Augument assignment versus regular assignment

Gerhard Fiedler gelists at gmail.com
Fri Jul 14 19:00:33 EDT 2006


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.

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

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

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


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

Thanks,
Gerhard




More information about the Python-list mailing list