extending yield's functionality

Michael Hudson mwh at python.net
Fri Nov 2 10:16:29 EST 2001


Eyal Lotem <eyal at hyperroll.com> writes:

> I was excited to find out about the new yield functionality, of
> generator functions, that will be formally in 2.3

Tim will presumably pop up momentarily and say:

    The title of PEP 255 is *simple* generators

but I'll do it for him anyway.  Worth bearing in mind.

> I wanted to ask if its possible to have yield receive some more
> information from the caller, between the yield stages.

I can't really understand this.

> An example syntax to implement this could look like:
> 
> >>> def a():
> ...     print "Hello"
> ...     b=yield 1
> ...     yield b*2
> ...     yield 3

Eww, that looks horrid.

> >>> gen = a()
> >>> gen.next()
> 1
> >>> gen.next(3)
> 6
> >>> gen.next()
> 3
> 
> This could make yield far more useful, and allow its use for more than just 
> simple generator functions. It could then be used to implement functions 
> that maintain a current-state through their internal flow, while using 
> event-driven programming.
> 
> Example: A negotiation protocol can be implemented using a loop of 
> recv/send operations, and yield the next stage's data each time, and accept 
> new external information from the event handler (perhaps the received data).

Can you post an example of what you mean?  I'm afraid your little
example above hasn't helped me understand what you want to (be able
to) do.

> Is this worth a PEP?

Hard to say just yet...

Cheers,
M.

-- 
  C++ is a siren song.  It *looks* like a HLL in which you ought to
  be able to write an application, but it really isn't.
                                       -- Alain Picard, comp.lang.lisp



More information about the Python-list mailing list