Created
November 6, 2018 17:16
-
-
Save phalt/d0c526ac6e7ad88320a3660b0f7a08f1 to your computer and use it in GitHub Desktop.
Context Manager and a decorator too!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from contextlib import ContextDecorator | |
class me_decorate(ContextDecorator): | |
def __init__(self, *args, **kwargs): | |
self.input_value = kwargs.get('keyword') | |
super(me_decorate, self).__init__() | |
def __enter__(self, *args, **kwargs): | |
print(self.input_value) | |
return self | |
def __exit__(self, *exc): | |
print(self.input_value) | |
return self | |
@me_decorate(keyword='value coming in') | |
def decorator_function(): | |
print('function value') | |
def context_function(): | |
with me_decorate(keyword='manager coming in'): | |
print('foo bar') | |
decorator_function() | |
context_function() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment