<div dir="ltr">You can use an ExitStack for this<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, May 18, 2016 at 1:29 AM, Giampaolo Rodola' <span dir="ltr"><<a href="mailto:g.rodola@gmail.com" target="_blank">g.rodola@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">This is one of those things which are so easy to implement which makes you think it is probably not worth adding to the stdlib, but then again, this is something I've ended up doing and rewriting pretty often over the years.<div>Real world example:<br><br><div>class DummyLock(object):</div><div><div>    def __init__(self, *args, **kwargs):</div><div>        pass</div><div>    def __enter__(self, *args, **kwargs):</div><div>        return self</div>    def __exit__(self, *args, **kwargs):<br>         pass</div><div><br></div><div>def get_lock(name, bypass_lock=False):</div><div>    lock_cls = DummyLock if bypass_lock else RedisLock</div><div>    return lock</div><div><br></div><div>with get_lock('foo', bypass_lock=True):<br>    ...</div><div><br></div><div>Similarly to contextlib.closing and contextlib.suppress, perhaps it would be nice to have contextlib.DummyContext just because it's something which is done (I think) fairly often.<br><br></div><div>On the bikeshedding front, in order to be consistent with closing(), redirect_stderr(), redirect_stdout() and suppress(), a better name for this would probably be contextlib.dummy_context.</div><div><br></div><div>Extra: the same thing can be achieved by using mock.MagicMock, which probably makes this proposal useless and kills it entirely. The additional value is that it would be more explicit/clear/immediate to have this in contextlib itself as opposed to unittest module, which is kinda weird. But then again, "there should be (possibly) only one way to do it" so I'm not sure. </div><div>OK, I should stop talking with myself. =) <span class="HOEnZb"><font color="#888888"><br></font></span></div><span class="HOEnZb"><font color="#888888"><div><br></div>-- <br><div><div dir="ltr"><div>Giampaolo - <a href="http://grodola.blogspot.com" target="_blank">http://grodola.blogspot.com</a></div><div><br></div></div></div>
</font></span></div></div>
<br>_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br></blockquote></div><br></div>