Last active
April 16, 2018 22:25
-
-
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
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 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