Re: revive a generator

2011-10-24 Thread alex23
On Oct 21, 12:09 pm, Yingjie Lan wrote: > Secondly, it would be nice to automatically revive it. Sure, it's always nice when your expectation of a language feature exactly matches with its capabilities. When it doesn't, you suck it up and code around it. Because at the very least it's a hell of

Re: revive a generator

2011-10-24 Thread alex23
On Oct 21, 11:46 am, Yingjie Lan wrote: > I am still not sure why should we enforce that  > a generator can not be reused after an explicit  > request to revive it? No one is "enforcing" anything, you're simply resisting implementing this yourself. Consider the following generator: import rand

Re: revive a generator

2011-10-22 Thread Carl Banks
On Thursday, October 20, 2011 6:23:50 AM UTC-7, Yingjie Lan wrote: > Hi, > > it seems a generator expression can be used only once: > > >>> g = (x*x for x in range(3)) > >>> for x in g: print x > 0 > 1 > 4 > >>> for x in g: print x #nothing printed > >>> > > Is there any way to revive g here? R

Re: revive a generator

2011-10-21 Thread Steven D'Aprano
On Fri, 21 Oct 2011 16:25:47 -0400, Terry Reedy wrote: > Here is a class that creates a re-iterable from any callable, such as a > generator function, that returns an iterator when called, + captured > arguments to be given to the function. > > class reiterable(): >def __init__(self, itercall

Re: revive a generator

2011-10-21 Thread Terry Reedy
Here is a class that creates a re-iterable from any callable, such as a generator function, that returns an iterator when called, + captured arguments to be given to the function. class reiterable(): def __init__(self, itercall, *args, **kwds): self.f = itercall # callable that returns a

Re: revive a generator

2011-10-21 Thread Ian Kelly
On Fri, Oct 21, 2011 at 2:02 AM, Yingjie Lan wrote: > Oops, my former reply has the code indentation messed up > by the mail system. Here is a reformatted one: > > > What if the generator involves a variable from another scope, > and before re-generating, the variable changed its value. > Also, th

Re: revive a generator

2011-10-21 Thread Steven D'Aprano
On Thu, 20 Oct 2011 19:09:42 -0700, Yingjie Lan wrote: >> Here's an example of an explicit request to revive the generator: > > > g = (x*x for x in range(3)) > for x in g: print x >> 0 >> 1 >> 4 > g = (x*x for x in range(3)) # revive the generator for x in g: > print x #now t

Re: revive a generator

2011-10-21 Thread Dave Angel
On 10/20/2011 10:09 PM, Yingjie Lan wrote: What if the generator is passed in as an argument when you are writing a function? That is, the expression is not available? Secondly, it would be nice to automatically revive it. For example, when another for-statement or other equivalent is appli

Re: revive a generator

2011-10-21 Thread Yingjie Lan
- Original Message - > From: Chris Angelico > To: python-list@python.org > Cc: > Sent: Friday, October 21, 2011 4:27 PM > Subject: Re: revive a generator > > On Fri, Oct 21, 2011 at 7:02 PM, Yingjie Lan wrote: >> What if the generator involves a

Re: revive a generator

2011-10-21 Thread Yingjie Lan
- Original Message - > From: Paul Rudin > > I'm not really sure whether you intend g to yield the original values > after your "revive" or new values based on the new value of vo.  But > still you can make a class that supports the iterator protocol and does > whatever you want (but you

Re: revive a generator

2011-10-21 Thread Chris Angelico
On Fri, Oct 21, 2011 at 7:02 PM, Yingjie Lan wrote: > What if the generator involves a variable from another scope, > and before re-generating, the variable changed its value. > Also, the generator could be passed in as an argument, > so that we don't know its exact expression. > There's actually

Re: revive a generator

2011-10-21 Thread Paul Rudin
Yingjie Lan writes: > > > What if the generator involves a variable from another scope, > and before re-generating, the variable changed its value. > Also, the generator could be passed in as an argument, > so that we don't know its exact expression. > vo = 34  g = (vo*x for x in range(3

Re: revive a generator

2011-10-21 Thread Yingjie Lan
- Original Message - > From: Paul Rudin > To: python-list@python.org > Cc: > Sent: Friday, October 21, 2011 3:27 PM > Subject: Re: revive a generator > > > The language has no explicit notion of a request to "revive" a > generator. You could use th

Re: revive a generator

2011-10-21 Thread Yingjie Lan
- Original Message - > From: Paul Rudin > The language has no explicit notion of a request to "revive" a > generator. You could use the same syntax to make a new generator that > yeilds the same values as the one you started with if that's what you >

Re: revive a generator

2011-10-21 Thread Paul Rudin
sure why should we enforce that  > a generator can not be reused after an explicit  > request to revive it? The language has no explicit notion of a request to "revive" a generator. You could use the same syntax to make a new generator that yeilds the same values as the one you started

Re: revive a generator

2011-10-21 Thread Yingjie Lan
> Here's an example of an explicit request to revive the generator: > g = (x*x for x in range(3)) for x in g: print x > 0 > 1 > 4 g = (x*x for x in range(3)) # revive the generator for x in g: print x #now this will work > 0 > 1 > 4 > > ChrisA What if the generator is p

Re: revive a generator

2011-10-20 Thread Chris Angelico
On Fri, Oct 21, 2011 at 12:46 PM, Yingjie Lan wrote: > > Thanks a lot to all who answered my question. > I am still not sure why should we enforce that > a generator can not be reused after an explicit > request to revive it? Here's an example of an explicit request to revive the generator: >>>

Re: revive a generator

2011-10-20 Thread Yingjie Lan
- Original Message - > From: Paul Rudin > To: python-list@python.org > Cc: > Sent: Thursday, October 20, 2011 10:28 PM > Subject: Re: revive a generator > > Yingjie Lan writes: > >> Hi, >> >> it seems a generator expression can be us

Re: revive a generator

2011-10-20 Thread Terry Reedy
On 10/20/2011 9:23 AM, Yingjie Lan wrote: it seems a generator expression can be used only once: Generators are iterators. Once iterators raise StopIteration, they are supposed to continue doing so. A generator expression defines a temporary anonymous generator function that is called once t

Re: revive a generator

2011-10-20 Thread Paul Rudin
Yingjie Lan writes: > Hi, > > it seems a generator expression can be used only once: > g = (x*x for x in range(3)) for x in g: print x > 0 > 1 > 4 for x in g: print x #nothing printed > > Is there any way to revive g here? > Generators are like that - you consume them until t

Re: revive a generator

2011-10-20 Thread Chris Angelico
On Fri, Oct 21, 2011 at 12:23 AM, Yingjie Lan wrote: > Hi, > > it seems a generator expression can be used only once: > g = (x*x for x in range(3)) for x in g: print x > 0 > 1 > 4 for x in g: print x #nothing printed > > Is there any way to revive g here? If you're not generat

revive a generator

2011-10-20 Thread Yingjie Lan
Hi, it seems a generator expression can be used only once: >>> g = (x*x for x in range(3)) >>> for x in g: print x 0 1 4 >>> for x in g: print x #nothing printed >>> Is there any way to revive g here? Yingjie -- http://mail.python.org/mailman/listinfo/python-list