Skip to content

Instantly share code, notes, and snippets.

@whoiscarlo
Last active April 16, 2018 22:25
Show Gist options
  • Save whoiscarlo/78ef39491c0f7dcce3f1e24835af80b6 to your computer and use it in GitHub Desktop.
Save whoiscarlo/78ef39491c0f7dcce3f1e24835af80b6 to your computer and use it in GitHub Desktop.
Creator a decorator that will run a command and even if it fails toggles a state on and off
from functools import wraps
def my_decorator(f):
@wraps(f)
def inner_dec(*args, **kws):
try:
## turn state on
f(*args, **kws)
print('test')
finally:
## turn state off
print('left')
return inner_dec
@my_decorator
def test():
print 's'+ 1
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment