list to table

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Nov 6 04:28:58 EST 2009


En Fri, 06 Nov 2009 04:29:05 -0300, Alf P. Steinbach <alfps at start.no>  
escribió:
> * Gabriel Genellina:
>> En Thu, 05 Nov 2009 21:23:27 -0300, Alf P. Steinbach <alfps at start.no>  
>> escribió:
>>
>>> foo()[bar()] += 1
>>>
> One reason was as mentioned that the C++ standard has essentially the  
> /same wording/ about "only evaluated once" but with a more strict  
> meaning; in C++, with the built-in += operator
>
>     a()[foo()] += 1;
>
> not only avoids calling a() and foo() twice, it also avoids doing the  
> internal indexing twice, while in python the internal indexing, locating  
> that item, is performed first in __getitem__ and then in __setitem__  
> (unless that is optimized away at lower level by caching last access,  
> but that in itself has overhead).

Yes, that's a common misunderstanding in people coming from other  
languages with a different semantics for "assignment" and "variable".  
You're not alone :)

Python does not have "lvalues" as in C++. In the statement x=1, the left  
hand side does not denote an object, but a name. x.attr=1 and x[index]=1  
act more like a function call (they *are* function calls actually) than  
assignments.

> Another reason was that §6.2 does explicitly discuss attribute  
> references as targets, but not subscription as target. It would have  
> been more clear to me if all (four?) possible target forms were  
> discussed. Happily you did now discuss that in the part that I snipped  
> above, but would've been nice, and easier for for an  
> other-language-thinking person :-), if it was in documentation.

Yes, probably that section should be improved (except the final example  
added, the text hasn't changed since it was first written, more than 9  
years ago).

Reading reference material may be terribly boring, I admit. Most people  
read only the specific sections required to solve a specific problem; and  
some concepts that are introduced earlier in the book are missed or  
skipped.
If you haven't already done so, try at least to read these two sections  
 from the Language Reference: 3.1. Objects, values and types, and 4.1.  
Naming and binding. They define the most important concepts in Python; the  
rest are just details.

-- 
Gabriel Genellina




More information about the Python-list mailing list