Normalizing A Vector

Lawrence D'Oliveiro ldo at geek-central.gen.new_zealand
Sat Jul 31 21:41:08 EDT 2010


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

> Lawrence D'Oliveiro <ldo at geek-central.gen.new_zealand> writes:
> 
>>     V = tuple \
>>       (
>>             x
>>         /
>>             l
>>        for x in V
>>        for l in
>>            (math.sqrt(reduce(lambda a, b : a + b, (y * y for y in V),
>>            0)),)
>>       )
> 
> You got the order wrong (it has to be for l ... for x ...)

No, I deliberately put it in that order to ensure that the value for l can 
only ever be evaulated once.

> You're kind of lucky here, because the arglist to tuple() provides a
> scope that hides x and l. Be careful if you ever change tuple(...) to
> [...], because x and l would leak to the outer scope (with python 2.*).

Interesting. However, using “list( ... )” instead of “[ ... ]” also prevents 
the leakage.



More information about the Python-list mailing list