[Python-Dev] Product iteration

Eric S. Raymond esr@thyrsus.com
Tue, 25 Jul 2000 18:08:18 -0400


Andrew Kuchling <akuchlin@mems-exchange.org>:
> On Tue, Jul 25, 2000 at 03:28:32PM -0400, Eric S. Raymond wrote:
> >[x**2 for x in filter(isprime, range(1, 99)]
>   ...
> >filter, zip, product, and lambda are good enough to cover anything the
> >presently proposed comprehension syntax can handle. 
> 
> ... but not so elegantly, if the filter needs variables from the
> current scope.  Consider removing all numbers in that range that are a
> multiple of a variable 'n'; you'd have to write:
> 
> [x**2 for x in filter(lambda x, number = n: (x%n)!=0, range(1,99)) ]
> 
> This uses the default argument hack to pass the value of 'n' into the
> scope of the lambda-created function as the parameter 'number'.
> Icky... I'd rather see extra syntax than more use of this kludge!

Wrong answer.  The right answer is to fix lambda (or some variant of lambda)
to be a true  lexical closure.
-- 
		<a href="http://www.tuxedo.org/~esr">Eric S. Raymond</a>

Question with boldness even the existence of a God; because, if there
be one, he must more approve the homage of reason, than that of
blindfolded fear.... Do not be frightened from this inquiry from any
fear of its consequences. If it ends in the belief that there is no
God, you will find incitements to virtue in the comfort and
pleasantness you feel in its exercise...
	-- Thomas Jefferson, in a 1787 letter to his nephew