diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-10-12 04:06:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-12 04:06:40 (GMT) |
commit | f6abb332a2cc9bf1027bf90637b98c606dc63b4a (patch) | |
tree | 177fbabe7b08566c07c10b2210e9617f29e54725 /Doc | |
parent | f94e6b4c42db0d73b910fc6a78f04afd7bbf7ad8 (diff) | |
download | cpython-f6abb332a2cc9bf1027bf90637b98c606dc63b4a.zip cpython-f6abb332a2cc9bf1027bf90637b98c606dc63b4a.tar.gz cpython-f6abb332a2cc9bf1027bf90637b98c606dc63b4a.tar.bz2 |
Formatting fixes in contextlib docs (GH-98111)
(cherry picked from commit 3b33c2010aa00ef5877bc35b02ae658e3c9f27af)
Co-authored-by: Stanley <46876382+slateny@users.noreply.github.com>
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/contextlib.rst | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/Doc/library/contextlib.rst b/Doc/library/contextlib.rst index 7c0b831..c656707 100644 --- a/Doc/library/contextlib.rst +++ b/Doc/library/contextlib.rst @@ -66,6 +66,8 @@ Functions and classes provided: # Code to release resource, e.g.: release_resource(resource) + The function can then be used like this:: + >>> with managed_resource(timeout=3600) as resource: ... # Resource is released at the end of this block, ... # even if code in the block raises an exception @@ -140,9 +142,9 @@ Functions and classes provided: finally: print(f'it took {time.monotonic() - now}s to run') - @timeit() - async def main(): - # ... async code ... + @timeit() + async def main(): + # ... async code ... When used as a decorator, a new generator instance is implicitly created on each function call. This allows the otherwise "one-shot" context managers @@ -249,15 +251,15 @@ Functions and classes provided: :ref:`asynchronous context managers <async-context-managers>`:: async def send_http(session=None): - if not session: - # If no http session, create it with aiohttp - cm = aiohttp.ClientSession() - else: - # Caller is responsible for closing the session - cm = nullcontext(session) + if not session: + # If no http session, create it with aiohttp + cm = aiohttp.ClientSession() + else: + # Caller is responsible for closing the session + cm = nullcontext(session) - async with cm as session: - # Send http requests with session + async with cm as session: + # Send http requests with session .. versionadded:: 3.7 @@ -379,6 +381,8 @@ Functions and classes provided: print('Finishing') return False + The class can then be used like this:: + >>> @mycontext() ... def function(): ... print('The bit in the middle') @@ -449,6 +453,8 @@ Functions and classes provided: print('Finishing') return False + The class can then be used like this:: + >>> @mycontext() ... async def function(): ... print('The bit in the middle') |