Pelokalan
Python ID
id en
Masuk
Proyek
library__contextlib.po Berkas proyek · python-docs-id-3.14 · Bahasa Indonesia
Modul PO Hanya lihat
Teks sumber — English
Terjemahan — Bahasa Indonesia
Significantly, ``aclosing()`` supports deterministic cleanup of async generators when they happen to exit early by :keyword:`break` or an exception. For example::
Return a context manager that returns *enter_result* from :meth:`~object.__enter__`, but otherwise does nothing. It is intended to be used as a stand-in for an optional context manager, for example::
For example::
Sebagai contoh::
For example, the output of :func:`help` normally is sent to *sys.stdout*. You can capture that output in a string by redirecting the output to an :class:`io.StringIO` object. The replacement stream is returned from the :meth:`~object.__enter__` method and so is available as the target of the :keyword:`with` statement::
For example, a set of files may easily be handled in a single with statement as follows::
For example, a group of files can be opened as an "all or nothing" operation as follows::
For example, it is sometimes useful to wrap functions or groups of statements with a logger that can track the time of entry and time of exit. Rather than writing both a function decorator and a context manager for the task, inheriting from :class:`ContextDecorator` provides both capabilities in a single definition::
Note also that being reentrant is *not* the same thing as being thread safe. :func:`redirect_stdout`, for example, is definitely not thread safe, as it makes a global modification to the system state by binding :data:`sys.stdout` to a different stream.