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 <1529493781.77.0.56676864532.issue33468@psf.upfronthosting.co.za>
In-reply-to
Content
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``.".
History
Date User Action Args
2018-06-20 11:23:01ncoghlansetrecipients: + ncoghlan, petr.viktorin, serhiy.storchaka
2018-06-20 11:23:01ncoghlansetmessageid: <1529493781.77.0.56676864532.issue33468@psf.upfronthosting.co.za>
2018-06-20 11:23:01ncoghlanlinkissue33468 messages
2018-06-20 11:23:01ncoghlancreate