[Python-ideas] Add contextlib.DummyContext
Joseph Jevnik
joejev at gmail.com
Wed May 18 01:34:23 EDT 2016
You can use an ExitStack for this
On Wed, May 18, 2016 at 1:29 AM, Giampaolo Rodola' <g.rodola at gmail.com>
wrote:
> 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.
> Real world example:
>
> class DummyLock(object):
> def __init__(self, *args, **kwargs):
> pass
> def __enter__(self, *args, **kwargs):
> return self
> def __exit__(self, *args, **kwargs):
> pass
>
> def get_lock(name, bypass_lock=False):
> lock_cls = DummyLock if bypass_lock else RedisLock
> return lock
>
> with get_lock('foo', bypass_lock=True):
> ...
>
> 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.
>
> 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.
>
> 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.
> OK, I should stop talking with myself. =)
>
> --
> Giampaolo - http://grodola.blogspot.com
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160518/5bccb104/attachment-0001.html>
More information about the Python-ideas
mailing list