Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Separate code definition and code execution
  • Loading branch information
slateny committed Oct 12, 2022
commit e66f8e95e0856e939ced3eb1ed21d15828abe962
52 changes: 30 additions & 22 deletions Doc/library/contextlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ Functions and classes provided:
# Code to release resource, e.g.:
release_resource(resource)

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
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

The function being decorated must return a :term:`generator`-iterator when
called. This iterator must yield exactly one value, which will be bound to
Expand Down Expand Up @@ -385,16 +387,19 @@ Functions and classes provided:

Example of ``ContextDecorator``::

>>> from contextlib import ContextDecorator
from contextlib import ContextDecorator

class mycontext(ContextDecorator):
def __enter__(self):
print('Starting')
return self

def __exit__(self, *exc):
print('Finishing')
return False

The class can then be used like this::

>>> class mycontext(ContextDecorator):
... def __enter__(self):
... print('Starting')
... return self
... def __exit__(self, *exc):
... print('Finishing')
... return False
...
>>> @mycontext()
... def function():
... print('The bit in the middle')
Expand Down Expand Up @@ -453,17 +458,20 @@ Functions and classes provided:

Example of ``AsyncContextDecorator``::

>>> from asyncio import run
>>> from contextlib import AsyncContextDecorator
from asyncio import run
from contextlib import AsyncContextDecorator

class mycontext(AsyncContextDecorator):
async def __aenter__(self):
print('Starting')
return self

async def __aexit__(self, *exc):
print('Finishing')
return False

The class can then be used like this::

>>> class mycontext(AsyncContextDecorator):
... async def __aenter__(self):
... print('Starting')
... return self
... async def __aexit__(self, *exc):
... print('Finishing')
... return False
...
>>> @mycontext()
... async def function():
... print('The bit in the middle')
Expand Down