Skip to content

Instantly share code, notes, and snippets.

@burakdede
Created September 30, 2016 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burakdede/8a74d502cfbb73e636ac5eb286c0819a to your computer and use it in GitHub Desktop.
Save burakdede/8a74d502cfbb73e636ac5eb286c0819a to your computer and use it in GitHub Desktop.
context manager with
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")
@burakdede
Copy link
Author

burakdede commented Sep 30, 2016

starting with block
starting with block
runningtest 2
runningtest 1
exited normally
exited normally

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment