[Python-ideas] Add contextlib.DummyContext

Giampaolo Rodola' g.rodola at gmail.com
Wed May 18 09:16:19 EDT 2016


On Wed, May 18, 2016 at 1:36 AM, Devin Jeanpierre <jeanpierreda 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):
> >     ...
>
> with contextlib.ExitStack() as exit_stack:
>   if not bypass_lock:
>     exit_stack.enter_context(RedisLock())
>
> And similar.
>
> In fact, ExitStack can itself be used as a no-op context manager, if you
> want.
>
> -- Devin
>

I didn't think about this. So yeah, my proposal is useless. Nevermind.



-- 
Giampaolo - http://grodola.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160518/410137ac/attachment.html>


More information about the Python-ideas mailing list