Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def context_decorator(cls, *args, **kwargs):
- def wrapper(self, function, *args, **kwargs):
- def inner(*args, **kwargs):
- cls.__enter__(self)
- result = function(*args, **kwargs)
- cls.__exit__(self, None, None, None)
- return result
- return inner
- cls.__call__ = wrapper
- return cls
- @context_decorator
- class PrinterContext:
- def __init__(self):
- pass
- def __enter__(self):
- print("Enter")
- def __exit__(self, exc_type, exc_val, exc_tb):
- print("Exit")
- return True
Add Comment
Please, Sign In to add comment