
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 <ncoghlan@gmail.com> Date: 2 October 2016 at 17:45 Subject: Re: [Python-ideas] Conditional context manager To: Neil Girdhar <mistersheik@gmail.com> On 2 October 2016 at 04:07, Neil Girdhar <mistersheik@gmail.com> wrote:
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() Resulting in: 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. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
participants (1)
-
Nick Coghlan