Conditional context manager
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() I'm not sure that this is possible within the grammar. (For some reason with with_expr contains '"as" expr' rather than '"as" NAME'? I realize this comes up somewhat rarely. I use context managers a lot, and it comes up maybe 1 in 5k lines of code. For some extensions of this notation, an else clause could bind a value to cm in the case that condition is false. Best, Neil
On 2016-10-01 19:07, Neil Girdhar 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()
I'm not sure that this is possible within the grammar. (For some reason with with_expr contains '"as" expr' rather than '"as" NAME'?
I realize this comes up somewhat rarely. I use context managers a lot, and it comes up maybe 1 in 5k lines of code.
For some extensions of this notation, an else clause could bind a value to cm in the case that condition is false.
If you defined a null context manager, you could then write: with (cm_function() if condition else cm_null()) as cm: suite() Do you need 'cm' itself? Its type changes depending on the condition, so I don't see how it could be useful. If it's not needed, then that shortens a little to: with cm_function() if condition else cm_null(): suite()
FYI: There is a null context manager: ExitStack(). On Sat, Oct 1, 2016 at 3:43 PM MRAB <python@mrabarnett.plus.com> wrote:
On 2016-10-01 19:07, Neil Girdhar 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()
I'm not sure that this is possible within the grammar. (For some reason with with_expr contains '"as" expr' rather than '"as" NAME'?
I realize this comes up somewhat rarely. I use context managers a lot, and it comes up maybe 1 in 5k lines of code.
For some extensions of this notation, an else clause could bind a value to cm in the case that condition is false.
If you defined a null context manager, you could then write:
with (cm_function() if condition else cm_null()) as cm: suite()
Do you need 'cm' itself? Its type changes depending on the condition, so I don't see how it could be useful.
If it's not needed, then that shortens a little to:
with cm_function() if condition else cm_null(): suite()
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
--
--- You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group. To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-ideas/dcu3O1qaC3E/unsubscribe. To unsubscribe from this group and all its topics, send an email to python-ideas+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
On Sun, Oct 2, 2016 at 5:07 AM, Neil Girdhar <mistersheik@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
participants (3)
-
Chris Angelico
-
MRAB
-
Neil Girdhar