Forwarding to the list, since I took the broken Google Group cc out of the reply list, but didn't added the proper one.
---------- Forwarded message ---------- From: Nick Coghlan email@example.com Date: 2 October 2016 at 17:45 Subject: Re: [Python-ideas] Conditional context manager To: Neil Girdhar firstname.lastname@example.org
On 2 October 2016 at 04:07, Neil Girdhar email@example.com wrote:
I'm just throwing this idea out there to get feedback.
Sometimes, I want to conditionally enter a context manager. This simplest (afaik) way of doing that is:
with ExitStack() as stack: if condition: cm = stack.enter_context(cm_function()) suite()
I suggest a more compact notation:
with cm_function() as cm if condition: suite()
As Chris notes, this is typically going to be better handled by creating an *un*conditional CM that encapsulates the optionality so you don't need to worry about it at the point of use.
If you wanted a generic version of that, then the stack creation and cm creation can be combined into a single context manager:
@contextmanager def optional_cm(condition, cm_factory, *args, **kwds): stack = ExitStack() cm = None with stack: if condition: cm = stack.enter_context(cm_factory(*args, **kwds)) yield stack, cm
However, even simpler than both this and Chris's maybe_cm() example is the plain ExitStack-as-the-null-context-manager function approach already covered in the contextlib docs: https://docs.python.org/3/library/contextlib.html#simplifying-support-for-si...
Applying that approach to this particular pattern looks like:
def optional_cm(condition, cm_factory, *args, **kwds): if condition: return cm_factory(*args, **kwds) return ExitStack()
with optional_cm(condition, cm_function): suite()
which is fine for a construct that is uncommon in general, but may be popular in a particular code base.
-- Nick Coghlan | firstname.lastname@example.org | Brisbane, Australia