[Python-ideas] Possible PEP 380 tweak

Nick Coghlan ncoghlan at gmail.com
Fri Oct 29 03:17:15 CEST 2010

On Fri, Oct 29, 2010 at 1:04 AM, Guido van Rossum <guido at python.org> wrote:
> On Thu, Oct 28, 2010 at 5:44 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> Yep, we've basically agreed on that as the way forward as well. We
>> have a small tweak to suggest for PEP 380 to avoid losing the return
>> value from inner close() calls,
> This is my "gclose()" function, right? Or is there more to it?

Yeah, the idea your gclose(), plus one extra tweak to the expansion of
"yield from" to store the result of the inner close() call on a new
GeneratorExit instance.

To use a toy example:

  # Even this toy framework needs a little structure
  class EndSum(Exception): pass

  def gsum():
    # Sums sent values until EndSum or GeneratorExit are thrown in
    tally = 0
      while 1:
        tally += yield
    except (EndSum, GeneratorExit):
    return x

  def average_sums():
    # Advances to a new sum when EndSum is thrown in
    # Finishes the last sum and averages them all when GeneratorExit
is thrown in
    sums = []
      while 1:
        sums.append(yield from gsum())
    except GeneratorExit as ex:
      # Our proposed expansion tweak is to enable the next line
    return sum(sums) / len(sums)


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

More information about the Python-ideas mailing list