Created
January 25, 2018 21:33
-
-
Save christabor/5a1d2b47214db8c123be510a7607dae0 to your computer and use it in GitHub Desktop.
decorators/ctxmgrs
This file contains 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 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