Skip to content

Instantly share code, notes, and snippets.

@barbeque
Created March 21, 2020 03:35
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 barbeque/900631f68f7bae455ed466726821a050 to your computer and use it in GitHub Desktop.
Save barbeque/900631f68f7bae455ed466726821a050 to your computer and use it in GitHub Desktop.
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