Context Manager
- Enter() should lock the resources and return an object.
- Exit() should release the resources.
+- Any exception that happens inside the with block is passed to the exit() method.
+- If it wishes to suppress the exception it must return a true value.
class MyOpen():
def __init__(self, filename):
self.filename = filename
def __enter__(self):
self.file = open(self.filename)
return self.file
- def __exit__(self, *args):
+ def __exit__(self, exc_type, exc_value, traceback):
self.file.close()