[Python-ideas] with None (Was: Re: Enhanced context managers with ContextManagerExit and None)
Masklinn
masklinn at masklinn.net
Tue Aug 13 21:30:52 CEST 2013
On 2013-08-13, at 21:19 , Serhiy Storchaka wrote:
> 13.08.13 18:29, Masklinn написав(ла):
>> If you want an optional context manager, why not create an
>> OptionalContextManager(object | None) which delegates to the underlying
>> non-None CM? That's simple, that's clear, that's explicit, and that's
>> not a hack.
>
> Because OptionalContextManager doesn't support an interface of the underlying object (e.g. doesn't have the read() method). And OptionalContextManager(None) is not None.
Irrelevant, OptionalContextManager's __enter__ returns either None or
whatever the underlying contextmanager's __enter__ yields. So the true
underlying object (or None) is available in the `with` body. OCM is
solely there to provide a NOOP context manager for a None input.
More information about the Python-ideas
mailing list