Re: revive a generator

2011-10-24 Thread alex23
On Oct 21, 11:46 am, Yingjie Lan lany...@yahoo.com 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:

Re: revive a generator

2011-10-24 Thread alex23
On Oct 21, 12:09 pm, Yingjie Lan lany...@yahoo.com 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

Re: revive a generator

2011-10-22 Thread Carl Banks
in xrange(3)) else: g = (x+x for x in xrange(3)) for y in g: print x revive(g) # which generator expression was it? # need to carry around a reference to be able to tell for y in g: print x Carrying a reference to a code object in turn carries around

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 passed in as an argument

Re: revive a generator

2011-10-21 Thread Paul Rudin
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 with if that's what you want. As we've already discussed if you want

Re: revive a generator

2011-10-21 Thread Yingjie Lan
- Original Message - From: Paul Rudin paul.nos...@rudin.co.uk 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 want. As we've

Re: revive a generator

2011-10-21 Thread Yingjie Lan
- Original Message - From: Paul Rudin paul.nos...@rudin.co.uk 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 the same syntax to make

Re: revive a generator

2011-10-21 Thread Paul Rudin
Yingjie Lan lany...@yahoo.com 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

Re: revive a generator

2011-10-21 Thread Chris Angelico
On Fri, Oct 21, 2011 at 7:02 PM, Yingjie Lan lany...@yahoo.com 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.

Re: revive a generator

2011-10-21 Thread Yingjie Lan
- Original Message - From: Paul Rudin paul.nos...@rudin.co.uk 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

Re: revive a generator

2011-10-21 Thread Yingjie Lan
- Original Message - From: Chris Angelico ros...@gmail.com 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 lany...@yahoo.com wrote: What if the generator involves a variable

Re: revive a generator

2011-10-21 Thread Dave Angel
On 10/20/2011 10:09 PM, Yingjie Lan wrote: snip 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

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 this will work 0 1 4

Re: revive a generator

2011-10-21 Thread Ian Kelly
On Fri, Oct 21, 2011 at 2:02 AM, Yingjie Lan lany...@yahoo.com 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

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

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,

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

Re: revive a generator

2011-10-20 Thread Chris Angelico
On Fri, Oct 21, 2011 at 12:23 AM, Yingjie Lan lany...@yahoo.com 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 generating

Re: revive a generator

2011-10-20 Thread Paul Rudin
Yingjie Lan lany...@yahoo.com 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 they run

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

Re: revive a generator

2011-10-20 Thread Yingjie Lan
- Original Message - From: Paul Rudin paul.nos...@rudin.co.uk To: python-list@python.org Cc: Sent: Thursday, October 20, 2011 10:28 PM Subject: Re: revive a generator Yingjie Lan lany...@yahoo.com writes: Hi, it seems a generator expression can be used only once: g

Re: revive a generator

2011-10-20 Thread Chris Angelico
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 -- http://mail.python.org/mailman/listinfo/python-list