This question is similar to Python: standard function and context manager?, but slightly different.
I have a number of classes, each of which defines a few @contextmanagers:
class MyClass:
@contextmanager
def begin_foo(param):
[some code]
yield
[some code]
[...]
I also have an API module that acts as a facade for the classes. In the API module I have found that I need to "wrap" the inner context managers so that I can use them as such in client code:
@contextmanager
def begin_foo(param):
with myobj.begin_foo(param):
yield
My client code looks like this right now:
# Case 1: Some things need to be done in the context of foo
with begin_foo(param):
[ stuff done in the context of foo ]
# Case 2: Nothing needs to be done in the context of foo --
# just run the entry and exit code
with begin_foo(param):
pass
Question: Is there a way I can use begin_foo as a standard function in the second case, without the need for the with ... pass construct? i.e. just do this:
# Case 2: Nothing needs to be done in the context of foo --
# just run the entry and exit code
begin_foo(param)
I can change the implementation of begin_foo in the API module and/or in the classes, if needed.