Normalizing A Vector

Lawrence D'Oliveiro ldo at geek-central.gen.new_zealand
Sun Aug 1 03:41:08 CEST 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