Conditional with statement in Python

前端 未结 8 1318
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 06:04

Is there a way to begin a block of code with a with statement, but conditionally?

Something like:

if needs_with():
    with get_stuff() as gs:

# do          


        
8条回答
  •  时光取名叫无心
    2020-11-28 06:19

    If you want to avoid duplicating code and are using a version of Python prior to 3.7 (when contextlib.nullcontext was introduced) or even 3.3 (when contextlib.ExitStack was introduced), you could do something like:

    class dummy_context_mgr():
        def __enter__(self):
            return None
        def __exit__(self, exc_type, exc_value, traceback):
            return False
    

    or:

    import contextlib
    
    @contextlib.contextmanager
    def dummy_context_mgr():
        yield None
    

    and then use it as:

    with get_stuff() if needs_with() else dummy_context_mgr() as gs:
       # do stuff involving gs or not
    

    You alternatively could make get_stuff() return different things based on needs_with().

    (See Mike's answer or Daniel's answer for what you can do in later versions.)

提交回复
热议问题