Nick Coghlan <[email protected]> added the comment:
Right, the HTML example was always a bit cutesy (hence the disclaimer in the
header), but it's hard to come up with a good illustrative example that isn't
already a native context manager in the standard library.
Perhaps it would make sense to change the example text completely, and write
something entirely abstract like:
from contextlib import contextmanager
@contextmanager
def managed_resource(*args, **kwds):
# Code to acquire resource, e.g.:
resource = acquire_resource(*args, **kwds)
try:
yield resource
finally:
# Code to release resource, e.g.:
release_resource(resource)
>>> with managed_resource(*args, **kwds) as resource:
... # Resource is released at the end of this block,
... # even if code in the block raises an exception
Then the introductory text could be updated to say something like "While many
objects natively support use in with statements, sometimes a resource needs to
be managed that isn't a context manager in its own right, and doesn't implement
a ``close()`` method for use with ``contextlib.closing``.".
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue33468>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com