lalala33rfs

Untitled

Nov 17th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. def context_decorator(cls, *args, **kwargs):
  2.     def wrapper(self, function, *args, **kwargs):
  3.         def inner(*args, **kwargs):
  4.             cls.__enter__(self)
  5.             result = function(*args, **kwargs)
  6.             cls.__exit__(self, None, None, None)
  7.             return result
  8.         return inner
  9.  
  10.     cls.__call__ = wrapper
  11.     return cls
  12.  
  13.  
  14. @context_decorator
  15. class PrinterContext:
  16.     def __init__(self):
  17.         pass
  18.  
  19.     def __enter__(self):
  20.         print("Enter")
  21.  
  22.     def __exit__(self, exc_type, exc_val, exc_tb):
  23.         print("Exit")
  24.         return True
Add Comment
Please, Sign In to add comment