Created
September 30, 2016 14:52
-
-
Save burakdede/8a74d502cfbb73e636ac5eb286c0819a to your computer and use it in GitHub Desktop.
context manager with
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
class TraceBlock: | |
def message(self, arg): | |
print("running" + arg) | |
def __enter__(self): | |
print("starting with block") | |
return self | |
def __exit__(self, exc_type, exc_value, exc_tb): | |
if exc_type is None: | |
print("exited normally\n") | |
else: | |
print("raise an exception! " + str(exc_type)) | |
return False # propogate | |
if __name__ == "__main__": | |
with TraceBlock() as action, TraceBlock() as action2: | |
action2.message("test 2") | |
action.message("test 1") |
Author
burakdede
commented
Sep 30, 2016
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment