Paul Rubin http://[EMAIL PROTECTED] writes:
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.
FWIW, here's a listcomp version:
def s2(n=100):
stream = range(2,n)
while stream:
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
Paul Du Bois [EMAIL PROTECTED] writes:
The second is that you don't like the late-binding behavior of
generator expressions. PEP 289 has this to say:
After much discussion, it was decided that the first (outermost)
for-expression should be evaluated immediately and that the remaining
Paul Rubin wrote:
Paul Du Bois [EMAIL PROTECTED] writes:
The second is that you don't like the late-binding behavior of
generator expressions. PEP 289 has this to say:
After much discussion, it was decided that the first (outermost)
for-expression should be evaluated immediately and that the
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
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
Ben Cartwright [EMAIL PROTECTED] 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
The generator is in its own scope. For proof, try accessing q outside
the generator.
There are two possibilities. The first is that you don't know what
closures are and are complaining that python has them. That would be
amusingly ironic, but I'm guessing you do know (if you don't, google