<div dir="ltr">FYI: There is a null context manager: ExitStack().</div><br><div class="gmail_quote"><div dir="ltr">On Sat, Oct 1, 2016 at 3:43 PM MRAB <<a href="mailto:python@mrabarnett.plus.com">python@mrabarnett.plus.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 2016-10-01 19:07, Neil Girdhar wrote:<br class="gmail_msg">
> I'm just throwing this idea out there to get feedback.<br class="gmail_msg">
><br class="gmail_msg">
> Sometimes, I want to conditionally enter a context manager.  This<br class="gmail_msg">
> simplest (afaik) way of doing that is:<br class="gmail_msg">
><br class="gmail_msg">
>     with ExitStack() as stack:<br class="gmail_msg">
>         if condition:<br class="gmail_msg">
>             cm = stack.enter_context(cm_function())<br class="gmail_msg">
>         suite()<br class="gmail_msg">
><br class="gmail_msg">
> I suggest a more compact notation:<br class="gmail_msg">
><br class="gmail_msg">
>     with cm_function() as cm if condition:<br class="gmail_msg">
>         suite()<br class="gmail_msg">
><br class="gmail_msg">
> I'm not sure that this is possible within the grammar.  (For some reason<br class="gmail_msg">
> with with_expr contains '"as" expr' rather than '"as" NAME'?<br class="gmail_msg">
><br class="gmail_msg">
> I realize this comes up somewhat rarely.  I use context managers a lot,<br class="gmail_msg">
> and it comes up maybe 1 in 5k lines of code.<br class="gmail_msg">
><br class="gmail_msg">
> For some extensions of this notation, an else clause could bind a value<br class="gmail_msg">
> to cm in the case that condition is false.<br class="gmail_msg">
><br class="gmail_msg">
If you defined a null context manager, you could then write:<br class="gmail_msg">
<br class="gmail_msg">
     with (cm_function() if condition else cm_null()) as cm:<br class="gmail_msg">
         suite()<br class="gmail_msg">
<br class="gmail_msg">
Do you need 'cm' itself? Its type changes depending on the condition, so<br class="gmail_msg">
I don't see how it could be useful.<br class="gmail_msg">
<br class="gmail_msg">
If it's not needed, then that shortens a little to:<br class="gmail_msg">
<br class="gmail_msg">
     with cm_function() if condition else cm_null():<br class="gmail_msg">
         suite()<br class="gmail_msg">
<br class="gmail_msg">
_______________________________________________<br class="gmail_msg">
Python-ideas mailing list<br class="gmail_msg">
<a href="mailto:Python-ideas@python.org" class="gmail_msg" target="_blank">Python-ideas@python.org</a><br class="gmail_msg">
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" class="gmail_msg" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br class="gmail_msg">
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" class="gmail_msg" target="_blank">http://python.org/psf/codeofconduct/</a><br class="gmail_msg">
<br class="gmail_msg">
--<br class="gmail_msg">
<br class="gmail_msg">
---<br class="gmail_msg">
You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.<br class="gmail_msg">
To unsubscribe from this topic, visit <a href="https://groups.google.com/d/topic/python-ideas/dcu3O1qaC3E/unsubscribe" rel="noreferrer" class="gmail_msg" target="_blank">https://groups.google.com/d/topic/python-ideas/dcu3O1qaC3E/unsubscribe</a>.<br class="gmail_msg">
To unsubscribe from this group and all its topics, send an email to <a href="mailto:python-ideas%2Bunsubscribe@googlegroups.com" class="gmail_msg" target="_blank">python-ideas+unsubscribe@googlegroups.com</a>.<br class="gmail_msg">
For more options, visit <a href="https://groups.google.com/d/optout" rel="noreferrer" class="gmail_msg" target="_blank">https://groups.google.com/d/optout</a>.<br class="gmail_msg">
</blockquote></div>