[Python-ideas] Revised**12 PEP on Yield-From

Nick Coghlan ncoghlan at gmail.com
Thu Apr 23 14:05:25 CEST 2009

Erik Groeneveld wrote:
> Fourthly, there is the issue of boundary clashes.  These are common in
> any data-processing problem.  The input data is simply not structured
> or tokenized according to boundaries on a certain level of
> abstraction.  This is the *very reason* to use coroutines and Jackson
> describes elegant ways to solve the problem.  JSP requires a lookahead
> and the coroutines must have some way to support this.  (Introducing a
> stream or buffer would put us back to where we started of course).
> After several tries I settled for a push-back mechanism as this was
> the most elegant way (IMHO) to solve it.  (This is why I suggested
> 'return value, pushbackdata').

Generators will be allowed to return tuples under the PEP, just like
normal functions. So what's wrong with doing something like the following?:

  def dummy_example():
    pushback = None
    while 1:
      item, pushback = yield from read_item(pushback)

  def read_item(init_data=None):
    if init_data is not None:
      # Initialise state based on init_data
     # Use default initial state
    # Read enough data to get a complete item
    # Since this is a coroutine, there will be at least 1 yield
    return item, excess_data


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list