Skip with statement in Python if condition is met

55 views Asked by At

Consider the following code:

if not condition:
    with my_object.whatever(...) as context_manager:
        do_something()

Is it possible to put the checking of condition inside my_object.whatever such that I don't need to wrap the with with an if every time? I am looking for an interface like this one:

with my_object.whatever(..., skip_with=condition) as context_manager:
    do_something()

but I would not know how to implement this. Maybe rising my own exception inside __enter__ and then catching it in the __exit__ ? I don't know if this would be a good practice.

0

There are 0 answers