How do I write a null (no-op) contextmanager in Python?

后端 未结 6 822
抹茶落季
抹茶落季 2021-02-01 12:58

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:

ctx_         


        
6条回答
  •  再見小時候
    2021-02-01 13:35

    Simple solution for Python 2.7, not yet mentioned in the answers:

    from contextlib import nested
    with nested():
        ...
    

提交回复
热议问题