PEP 255: Simple Generators

Just van Rossum just at letterror.com
Wed Jun 20 03:30:35 EDT 2001


Olaf Delgado Friedrichs wrote:

> If I understand correctly, this should work:
>
>  def f():
>    for i in range(5):
>      for x in g(i):
>        yield x
>
>  def g(i):
>    for j in range(10):
>      yield i,j

Greg Ewing wrote:

> Yes, I realised that shortly afterwards. But I think
> we're going to get a lot of questions from newcomers
> who have tried to implicitly nest iterators and are
> very confused about why it doesn't work and what needs
> to be done to make it work.

Erm, "yield" by definition yields to its immediate caller,
so how could there be any confusion?

I'm personally still not convinced that a generator keyword
would be a huge improvement. Here's how I see it: once you
see a "yield" statement it means the function returns not
one but an arbitrary amount of values, which you can iterate
over. 

The next two functions are more or less equivalent:

def returnThreeValues():
	return 1, 2, 3

def generateThreeValues():
	yield 1
	yield 2
	yield 3

Just



More information about the Python-list mailing list