[Python-ideas] Possible PEP 380 tweak

Guido van Rossum guido at python.org
Tue Oct 26 05:25:08 CEST 2010


By the way, here's how to emulate the value-returning-close() on a
generator, assuming the generator uses raise StopIteration(x) to mean
return x:

def gclose(gen):
  try:
    gen.throw(GeneratorExit)
  except StopIteration, err:
    if err.args:
      return err.args[0]
  except GeneratorExit:
    pass
  return None

I like this because it's fairly straightforward (except for the detail
of having to also catch GeneratorExit).

In fact it would be a really simple change to gen_close() in
genobject.c -- the only change needed there would be to return
err.args[0]. I like small evolutionary improvements to APIs.

-- 
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list