[Python-Dev] Product iteration

Andrew Kuchling akuchlin@mems-exchange.org
Tue, 25 Jul 2000 15:37:06 -0400


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!

--amk