The with statement (__enter__ & _exit__ methods)¶
Many (modern) classes have an __enter__
and an __exit__
method; this pair is used by the
with-statement. And are used to create a context manager.
Before executing the block of statements in the the with-statements, the __enter__()
of
the object named in the with-statement is called. And, when leaving that block the
__exit__()
is called, always! This is convenient, as it cleans up the code “within”.
Tip
It may be convenient to think placeholder to “open” and “close” the object.
As the with-statement is often used with a newly initiated object, part of the “before” code is often located in the
__init__()
; leaving onlyreturn self
in the __init__.The
__exit__
has some extra parameters, that describe “how” the block is exited. Without a raised exception, they are None; else they contain the “context” of that exception.For the design-analyse, they are not that important. Every design can be coded without them!