Meta: PEP discussion (was Re: PEP 255: Simple Generators)

Just van Rossum just at letterror.com
Wed Jun 27 05:16:00 EDT 2001


Robin Becker wrote:
> it would have been so much nicer and easier if these generator functions
> could be generalised to be resumable then all the nonsense about where
> and when they could be used would vanish. This seems to be too late now.

Eh? This *is* generalized: you can resume a generator "by hand":

  >>> def gen(x):
  ... 	i = 0
  ... 	while i < x:
  ... 		yield i
  ... 		i += 1
  ... 
  >>> g = gen(4)
  >>> g.next()
  0
  >>> g.next()
  1
  >>> g.next()
  2
  >>> g.next()
  3
  >>> g.next()
  Traceback (most recent call last):
    File "<input>", line 1, in ?
  StopIteration
  >>> 

The fact that this uses the iterator protocol is merely because it's practical:
most often generators will be used in a for context.

Just



More information about the Python-list mailing list