msg151530 - (view) |
Author: Arkadiusz Wahlig (yak) |
Date: 2012-01-18 10:43 |
Generators should support the with statement with __exit__ calling self.close(). with genfunc() as g: for item in g: print(item) |
|
|
msg151532 - (view) |
Author: Ezio Melotti (ezio.melotti) *  |
Date: 2012-01-18 10:57 |
If you want to call .close() automatically on something you can use contextlib.closing(): http://docs.python.org/library/contextlib.html#contextlib.closing |
|
|
msg151717 - (view) |
Author: Terry J. Reedy (terry.reedy) *  |
Date: 2012-01-21 04:03 |
Calling g.close() is pointless for a generator used in normal pull mode and run to completion, as in the example. The generator is already 'closed', so g.close() does not do anything useful. See http://docs.python.org/py3k/reference/expressions.html#yield-expressions The method was added to be used with .send() so that generators used in push mode could be told to finish up when there is nothing more to send. For such rare uses, contextlib.closing should usually be sufficient, I think. So I think this issue should be closed. |
|
|
msg151763 - (view) |
Author: Alyssa Coghlan (ncoghlan) *  |
Date: 2012-01-22 06:58 |
Generators deliberately don't support the context management protocol. This is so that they raise an explicit TypeError or AttributeError (pointing out that __exit__ is missing) if you leave out the @contextmanager decorator when you're using a generator to write an actual context manager. Generators supporting the context management protocol natively would turn that into a far more subtle (and confusing) error: your code would silently fail to invoke the generator body. Ensuring this common error remains easy to detect is far more important than making it easier to invoke close() on a generator object (particularly when contextlib.closing() already makes that very easy). |
|
|
msg240820 - (view) |
Author: R. David Murray (r.david.murray) *  |
Date: 2015-04-14 00:54 |
Looks like the right approach, I hadn't thought of the design FAQ, but it makes sense as a place to put it. I made a couple of review comments. |
|
|
msg396285 - (view) |
Author: Terry J. Reedy (terry.reedy) *  |
Date: 2021-06-21 19:54 |
I added a simplified answer after a similar question about assignment and CMs. |
|
|
msg396290 - (view) |
Author: Terry J. Reedy (terry.reedy) *  |
Date: 2021-06-21 21:23 |
New changeset 51f45d085dad3b708f6fe166af517aba69e7e9f7 by Terry Jan Reedy in branch 'main': bpo-13814: Explain why generators are not context managers (GH-26835) https://github.com/python/cpython/commit/51f45d085dad3b708f6fe166af517aba69e7e9f7 |
|
|
msg396293 - (view) |
Author: Terry J. Reedy (terry.reedy) *  |
Date: 2021-06-21 22:02 |
New changeset 1e16217204c0e8e595c4d1e869c81899bfe3376b by Miss Islington (bot) in branch '3.10': bpo-13814: Explain why generators are not context managers (GH-26835) https://github.com/python/cpython/commit/1e16217204c0e8e595c4d1e869c81899bfe3376b |
|
|
msg396294 - (view) |
Author: Terry J. Reedy (terry.reedy) *  |
Date: 2021-06-21 22:03 |
New changeset d881002fbdf12ddbd93db3e182dc5cdeb1f90386 by Miss Islington (bot) in branch '3.9': bpo-13814: Explain why generators are not context managers (GH-26835) https://github.com/python/cpython/commit/d881002fbdf12ddbd93db3e182dc5cdeb1f90386 |
|
|
msg396552 - (view) |
Author: Terry J. Reedy (terry.reedy) *  |
Date: 2021-06-26 14:49 |
Note: Rietveld patch reviews are no longer accessible so I could not look at R. David's comments. |
|
|