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: with freeze_time(test_dt): lines_of_code_1 lines_of_code_2 lines_of_code_3 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: if test_dt: with freeze_time(test_dt): lines_of_code_1 lines_of_code_2 lines_of_code_3 else: lines_of_code_1 lines_of_code_2 lines_of_code_3 Assume that lines_of_code here is 2-3 lines of code which are exactly identical, is there a cleaner way of writing this? I'm aware that I could write