Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
86e78d1f
Commit
86e78d1f
authored
Jul 18, 2010
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#9110: update to ContextDecorator doc.
parent
02053ee3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
13 deletions
+32
-13
Doc/library/contextlib.rst
Doc/library/contextlib.rst
+32
-13
No files found.
Doc/library/contextlib.rst
View file @
86e78d1f
...
...
@@ -57,6 +57,7 @@ Functions provided:
.. versionchanged:: 3.2
Use of :class:`ContextDecorator`.
.. function:: closing(thing)
Return a context manager that closes *thing* upon completion of the block. This
...
...
@@ -92,22 +93,25 @@ Functions provided:
``__enter__`` and ``__exit__`` as normal. ``__exit__`` retains its optional
exception handling even when used as a decorator.
Example::
``ContextDecorator`` is used by :func:`contextmanager`, so you get this
functionality automatically.
Example of ``ContextDecorator``::
from contextlib import ContextDecorator
class mycontext(ContextDecorator):
def __enter__(self):
print('Starting')
return self
def __enter__(self):
print('Starting')
return self
def __exit__(self, *exc):
print('Finishing')
return False
def __exit__(self, *exc):
print('Finishing')
return False
>>> @mycontext()
... def function():
... print('The bit in the middle')
...
print('The bit in the middle')
...
>>> function()
Starting
...
...
@@ -115,23 +119,38 @@ Functions provided:
Finishing
>>> with mycontext():
... print('The bit in the middle')
...
print('The bit in the middle')
...
Starting
The bit in the middle
Finishing
This change is just syntactic sugar for any construct of the following form::
def f():
with cm():
# Do stuff
``ContextDecorator`` lets you instead write::
@cm()
def f():
# Do stuff
It makes it clear that the ``cm`` applies to the whole function, rather than
just a piece of it (and saving an indentation level is nice, too).
Existing context managers that already have a base class can be extended by
using ``ContextDecorator`` as a mixin class::
from contextlib import ContextDecorator
class mycontext(ContextBaseClass, ContextDecorator):
def __enter__(self):
return self
def __enter__(self):
return self
def __exit__(self, *exc):
return False
def __exit__(self, *exc):
return False
.. versionadded:: 3.2
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment