Class Variable Access and Assignment

Antoon Pardon apardon at forel.vub.ac.be
Mon Nov 7 03:47:44 EST 2005


Op 2005-11-04, Christopher Subich schreef <csubich.spam.block at spam.subich.block.com>:
> Antoon Pardon wrote:
>> Well maybe because as far as I understand the same kind of logic
>> can be applied to something like
>> 
>> lst[f()] += foo
>> 
>> In order to decide that this should be equivallent to
>> 
>> lst[f()] = lst[f()] + foo.
>> 
>> But that isn't the case.
>
> Because, surprisingly enough, Python tends to evaluate expressions only 
> once each time they're invoked.

Well but once can consider b.a as an expression too. An expression
that gets evaluated twice in case of

b.a += 2

> In this case, [] is being used to get an item and set an item -- 
> therefore, it /has/ to be invoked twice -- once for __getitem__, and 
> once for __setitem__.

But we are here questioning language design. One could question a design
where it is necessary to invoke the [] operator twice, even when it
is only mentioned once in the code.

> Likewises, lst appears once, and it is used once -- the name gets looked 
> up once (which leads to a += 1 problems if a is in an outer scope).
>
> f() also appears once -- so to evaluate it more than one time is odd, 
> at best.

No more or less than "[]" or "." is to be invoked twice. 

-- 
Antoon Pardon



More information about the Python-list mailing list