Nick Coghlan <ncogh...@gmail.com> added the comment: 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). ---------- assignee: -> docs@python components: +Documentation nosy: +docs@python stage: test needed -> needs patch status: pending -> open title: Generators as context managers. -> Document why generators don't support the context management protocol _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue13814> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com