Normalizing A Vector

Lawrence D'Oliveiro ldo at geek-central.gen.new_zealand
Sun Aug 1 20:41:04 EDT 2010


In message <87k4oah1rp.fsf at dpt-info.u-strasbg.fr>, Alain Ketterlin wrote:

> Lawrence D'Oliveiro <ldo at geek-central.gen.new_zealand> writes:
> 
>> No, I deliberately put it in that order to ensure that the value for l
>> can only ever be evaulated once.
> 
> Try this (essentially equivalent to your code):
> 
> def f():
>     print "hello"
>     return 1
> 
> V = tuple( 1 for x in [1,2,3] for l in (f(),) )
> 
> How many "hello"s do you see?
> 
> Comprehension are not loops spelled backwards. The values in:
> 
>     whatever for i ... for j ...
> 
> are the values produced by the equivalent code:
> 
>     for i ...
>         for j ...
>             whatever

Damn. I thought that

    e for i ... for j ...

was equivalent to

    (e for i ...) for j ...

Looks like I was wrong.




More information about the Python-list mailing list