How do I write a null (no-op) contextmanager in Python? : <p>Sometimes I need a dummy context manager that does nothing. It can then be used as a stand-in for a more useful, but optional, context manager. For example:</p>

<pre><code>ctx_mgr = &lt;meaningfulContextManager&gt; if &lt;condition&gt; else &lt;nullContextManager&gt;
with ctx_mgr:
    ...
</code></pre>

<p>How do I define such a trivial, empty context manager? Does the Python library offer one off the shelf?</p>

<p>How about cases where we want the context to be used with an <code>as</code> clause?</p>

<pre><code>with ctx_mgr as resource:
    &lt;operations on resource&gt;
</code></pre>
