Skip to content

Instantly share code, notes, and snippets.

@christabor
Created January 25, 2018 21:33
Embed
What would you like to do?
decorators/ctxmgrs
from contextlib import contextmanager
def to_mgr(fn):
fn.__converted__ = True
@contextmanager
def fn2(*args, **kwargs):
try:
fn(*args, **kwargs)
yield
except Exception:
pass
finally:
print('DOne')
return fn2
@to_mgr
def greetings(name='anon'):
print('HEEEY, {}'.format(name))
print('HEee2')
if __name__ == '__main__':
with greetings(name='me'):
print('Hello back')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment