genexp surprise (wart?)
Michele Simionato
michele.simionato at gmail.com
Fri May 26 04:46:15 EDT 2006
Paul Rubin wrote:
> I tried to code the Sieve of Erastosthenes with generators:
>
> def sieve_all(n = 100):
> # yield all primes up to n
> stream = iter(xrange(2, n))
> while True:
> p = stream.next()
> yield p
> # filter out all multiples of p from stream
> stream = (q for q in stream if q%p != 0)
>
> # print primes up to 100
> print list(sieve_all(100))
>
> but it didn't work.
This is a known issue with the scope rules in loops which has bitten
many (including
myself; there is a long thread involving me and Jacek Generowitz
debating this at
death, you may find it if you google the newsgroup).
I would be curious to know if your code would work the way you expect
in Haskell
(I know it would for 'for' loop, dunno about 'while' loops, I am
completely ignorant
in Haskell).
Michele Simionato
More information about the Python-list
mailing list