<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. =) <br></div><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div>Giampaolo - <a href="http://grodola.blogspot.com" target="_blank">http://grodola.blogspot.com</a></div><div><br></div></div></div>
</div></div>