[Python] Handling instances of a context manager inside another context manager

Discussão em 'Python' iniciado por Stack, Fevereiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    How should a context manager created inside another context manager be handled in Python?

    Example: suppose you have class A that acts as a context manager, and class B that also acts as a context manager. But class B instances will have to instantiate and use an instance of class A. I've gone through PEP 343 and this is the solution I thought of:

    class A(object):
    def __enter__(self):
    # Acquire some resources here
    return self

    def __exit__(seplf, exception_type, exception, traceback):
    # Release the resources and clean up
    pass


    class B(object):
    def __init__(self):
    self.a = A()

    def __enter__(self):
    # Acquire some resources, but also need to "start" our instance of A
    self.a.__enter__()
    return self

    def __exit__(self, exception_type, exception, traceback):
    # Release the resources, and make our instance of A clean up as well
    self.a.__exit__(exception_type, exception, traceback)


    Is this the correct approach? Or am I missing some gotchas?

    Continue reading...

Compartilhe esta Página