This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ncoghlan
Recipients ncoghlan, petr.viktorin, serhiy.storchaka
Date 2018-06-20.11:23:01
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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

    def managed_resource(*args, **kwds):
        # Code to acquire resource, e.g.:
        resource = acquire_resource(*args, **kwds)
            yield resource
             # Code to release resource, e.g.:

    >>> 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``.".
Date User Action Args
2018-06-20 11:23:01ncoghlansetrecipients: + ncoghlan, petr.viktorin, serhiy.storchaka
2018-06-20 11:23:01ncoghlansetmessageid: <>
2018-06-20 11:23:01ncoghlanlinkissue33468 messages
2018-06-20 11:23:01ncoghlancreate