I understand that
__enter__and__exit__are used to implement a context manager.if an exception occurs in a
withstatement, the exception's type, value and traceback are passed to the__exit__method.__exit__can handle the exception:- Returning
True: the exception is gracefully handled. - Returning anything else: the
withstatement raises the exception
- Returning
I came across the following __exit__ method. Is the return statement redundant?
def __exit__(self, type, value, traceback):
self.close()
return type == None
since it seems to me that,
- If no exception occurred,
typewill naturally beNone, so__exit__returns true. Nothing is raised. - If an exception did occur,
typeis set to the actual exception type, so__exit__returns false. The exception is raised as is.