On Wed, May 18, 2016 at 1:36 AM, Devin Jeanpierre firstname.lastname@example.org wrote:
This is one of those things which are so easy to implement which makes
think it is probably not worth adding to the stdlib, but then again,
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())
In fact, ExitStack can itself be used as a no-op context manager, if you want.
I didn't think about this. So yeah, my proposal is useless. Nevermind.