Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
how to implement behaviour of 'with' blocks in python
class Exiter():
def __enter__(self):
print('i am in a block')
return self # critical
def __exit__(self, type, value, tb):
print('goodbye cruel world')
def add(self, a, b):
return a + b
with Exiter() as exiter:
val = exiter.add(5, 10)
print('the sum of 5 and 10 is ' + str(val))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment