Re: genexp surprise (wart?)

2006-05-26 Thread Mel Wilson
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 immediat

Re: genexp surprise (wart?)

2006-05-26 Thread Paul Rubin
"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 remai

Re: genexp surprise (wart?)

2006-05-26 Thread Michele Simionato
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 multi

Re: genexp surprise (wart?)

2006-05-25 Thread Paul Rubin
Paul Rubin 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:

Re: genexp surprise (wart?)

2006-05-25 Thread Paul Du Bois
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 "make_ad

Re: genexp surprise (wart?)

2006-05-25 Thread Paul Rubin
"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, t

Re: genexp surprise (wart?)

2006-05-25 Thread Ben Cartwright
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 multi

genexp surprise (wart?)

2006-05-25 Thread Paul Rubin
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 s