[Python-ideas] Conditional context manager
Chris Angelico
rosuav at gmail.com
Sat Oct 1 18:45:41 EDT 2016
On Sun, Oct 2, 2016 at 5:07 AM, Neil Girdhar <mistersheik at gmail.com> wrote:
> I suggest a more compact notation:
>
> with cm_function() as cm if condition:
> suite()
>
The simplest way would be to make a conditional version of the context manager.
@contextlib.contextmanager
def maybe_cm(state):
if state:
with cm_function() as cm:
yield cm
else:
yield None
I believe that'll work.
ChrisA
More information about the Python-ideas
mailing list