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 protected]> Date: 2 October 2016 at 17:45 Subject: Re: [Python-ideas] Conditional context manager To: Neil Girdhar <[email protected]> On 2 October 2016 at 04:07, Neil Girdhar <[email protected]> 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-single-optional-context-managers 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 | [email protected] | Brisbane, Australia -- Nick Coghlan | [email protected] | Brisbane, Australia _______________________________________________ Python-ideas mailing list [email protected] https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
