genexp surprise (wart?)

Paul Rubin http
Fri May 26 07:44:05 CEST 2006


"Ben Cartwright" <bencvt at gmail.com> writes:
> You do realize that you're creating a new level of generator nesting
> with each iteration of the while loop, right?  You will quickly hit the
> maximum recursion limit.  Try generating the first 1000 primes.

Yes, I know about the generator nesting, that was the point.  The
Sieve of Eraswhatsisname takes linear space by definition.  It's
usually done with an array but this version uses nested generators
instead.  I didn't bother to test whether they counted against the
recursion limit, so it's informative to know that they do.

> I don't see how it's a wart.  p is accessed (i.e., not set) by the
> genexp.  Consistent with the function scoping rules in...
> http://www.python.org/doc/faq/programming/#what-are-the-rules-for-local-and-global-variables-in-python
> ...Python treats p in the genexp as a non-local variable.

Yeah, it's just counterintuitive is all.  I guess the natural way to
express this would have been with tail recursion instead of a while
loop.



More information about the Python-list mailing list