|
Project Information
Links
|
If you're a library or framework creator then it is nice to be able to create APIs that can be used either as decorators or context managers. The contextdecorator module is a backport of new features added to the contextlib module in Python 3.2. contextdecorator works with Python 2.4+ including Python 3. Context managers inheriting from ContextDecorator have to implement __enter__ and __exit__ as normal. __exit__ retains its optional exception handling even when used as a decorator. Example: from contextdecorator import ContextDecorator
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'
with mycontext():
print 'The bit in the middle'Existing context managers that already have a base class can be extended by using ContextDecorator as a mixin class from contextdecorator import ContextDecorator
class mycontext(ContextBaseClass, ContextDecorator):
def __enter__(self):
return self
def __exit__(self, *exc):
return Falsecontextdecorator also contains an implementation of contextlib.contextmanager that uses ContextDecorator. The context managers it creates can be used as decorators as well as in statements. |