Last active
April 1, 2018 07:39
-
-
Save sykwer/cf493b1a8e89db6938339dc1a094fc41 to your computer and use it in GitHub Desktop.
Simplest snippet that investigates how "with" works 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 Klass: | |
def __init__(self): | |
print("instanciated") | |
def __del__(self): | |
print("gabage collected") | |
def __enter__(self): | |
print("__enter__") | |
return self | |
def __exit__(self, execution_type, execution_value, traceback): | |
print("__exit__") | |
self.close() | |
print("execution type: ", execution_type) | |
print("execution value: ", execution_value) | |
print("traceback: ", traceback) | |
def do_something(self): | |
print("do something") | |
def close(self): | |
print("close!!1!!") | |
def main(): | |
with Klass() as instance: | |
instance.do_something() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment