return self def __exit__(selfexc_typeexc_valueexc_tb)if exc_type is noneprint('exited normally\ 'elseprint('raise an exceptionstr(exc_type)return false propagate if __name__ ='__main__'with traceblock(as actionaction message('test 'print('reached'with traceblock(as actionaction message('test 'raise typeerror print('not reached'notice that this class' __exit__ method returns false to propagate the exceptiondeleting the return statement would have the same effectas the default none return value of functions is false by definition also notice that the __enter__ method returns self as the object to assign to the as variablein other use casesthis might return completely different object instead when runthe context manager traces the entry and exit of the with statement block with its __enter__ and __exit__ methods here' the script in action being run under either python or (as usualmileage varies slightly in some displaysand this runs on and if enabled) :\codepy - withas py starting with block running test reached exited normally starting with block running test raise an exceptiontraceback (most recent call last)file "withas py"line in raise typeerror typeerror context managers can also utilize oop state information and inheritancebut are somewhat advanced devices for tool buildersso we'll skip additional details here (see python' standard manuals for the full story--for examplethere' new contextlib standard module that provides additional tools for coding context managersfor simpler purposesthe try/finally statement provides sufficient support for terminationtime activities without coding classes with/as context managers