Skip to content

Instantly share code, notes, and snippets.

@sykwer
Last active April 1, 2018 07:39
Show Gist options
  • Save sykwer/cf493b1a8e89db6938339dc1a094fc41 to your computer and use it in GitHub Desktop.
Save sykwer/cf493b1a8e89db6938339dc1a094fc41 to your computer and use it in GitHub Desktop.
Simplest snippet that investigates how "with" works in Python.
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