Skip to content

Tag: contextmanager

Conditional or optional context managers in with statement

Suppose I have some kind of context manager (from a third-party library) that I am using like so: But, suppose if there is no value for test_dt, the context manager should not run, but all of the remaining code should run, like so: Assume that lines_of_code here is 2-3 lines of code which are exactly identica…