class Resource:

    def __init__(self, dependency=None):
        self.dependency = dependency

def __enter__(self):
    if self.dependency:
        self.dependency.__enter__()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
    if self.dependency:
        self.dependency.__exit__(exc_type, exc_val, exc_tb)
Handling instances of a context manager inside another context manager