Created
March 21, 2020 03:35
-
-
Save barbeque/900631f68f7bae455ed466726821a050 to your computer and use it in GitHub Desktop.
how to implement behaviour of 'with' blocks in python
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 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