Skip to content

Instantly share code, notes, and snippets.

@phalt
Created November 6, 2018 17:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phalt/d0c526ac6e7ad88320a3660b0f7a08f1 to your computer and use it in GitHub Desktop.
Save phalt/d0c526ac6e7ad88320a3660b0f7a08f1 to your computer and use it in GitHub Desktop.
Context Manager and a decorator too!
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