[Python-ideas] Possible PEP 380 tweak (original) (raw)
Guido van Rossum guido at python.org
Fri Oct 29 16:28:29 CEST 2010
- Previous message: [Python-ideas] Possible PEP 380 tweak
- Next message: [Python-ideas] Possible PEP 380 tweak
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Thu, Oct 28, 2010 at 10:25 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
Guido van Rossum wrote:
I'd also like to convince you to change g.close() so that it captures and returns the return value from StopIteration if it has one. Looking at this again, I find that I'm not really sure how this impacts PEP 380. The current expansion specifies that when a delegating generator is closed, the subgenerator's close() method is called, any value it returns is ignored, and GeneratorExit is re-raised. If that close() call were to return a value, what do you think should be done with it?
I went over that myself in detail and ended up deciding that for "yield-from" nothing should be changed! The expansion in the PEP remains the same.
But since this PEP also specifies "return value" it would be nice if there was a convenient way to capture this value, and close seems to be it. E.g.
def gen(): total = 0 try: while True: total += yield except GeneratorExit: return total
def main(): g = gen() for i in range(100): g.send(i) print(g.close())
This would print the total computed by gen().
-- --Guido van Rossum (python.org/~guido)
- Previous message: [Python-ideas] Possible PEP 380 tweak
- Next message: [Python-ideas] Possible PEP 380 tweak
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]