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