PEP 255: Simple Generators

Roman Suzi rnd at onego.ru
Wed Jun 20 04:35:35 EDT 2001


On Wed, 20 Jun 2001, Just van Rossum wrote:

Hello!

Let me express my view on this.

I think, that in itself this scheme is good.

However if we look at it broder, PEP255 is only half of the solution.

Far better solution could be to built-in parallel programming. like, for
example, in Occam, which is more general approach to build not only
generators but arbitrary asynchronous object interactions _built-in_ into
the language.

yield is a pipe between two functions where one function stops to be
started again when the other calls. It is narrow case. Pipes
could be used wider than in the "yield" example!

Even without parallel programming, pipes could be somehow registered and
used with objects or functions in more general way, with iterator protocol
worded explicitly, not built-into yield.

Far better could be to allow arbitrary pipes between objects...

But I guess Python internals aren't ready for parallel programming, so
send/receive (with or without queues) could not supported transparently
yet.

These ideas are theoretical. In my work I do not use parallel programming
inside the program level. So do not hesitate to call ideas above nonsense.
But I feel that PEP255 could cover more general pipes than
generator/consumer...

Another question about yield is how do I have it with objects?
Will there be __yield__ (similar to __add__ or __del__)?
Could I yield from methods? (and why not?)

> > 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
> 

Sincerely yours, Roman A.Suzi
-- 
 - Petrozavodsk - Karelia - Russia - mailto:rnd at onego.ru -
 





More information about the Python-list mailing list