문제

Given a class

class SomeClass:
    @contextlib.contextmanager
    def on_connection(self, target_terminal, source_terminal):
        ...
        yield
        ...

How do you inherit from it?

도움이 되었습니까?

해결책

It's possible to combine the contextlib.contextmanager pattern with a with block to bring in the superclass' context manager:

class SomeDerivedClass(SomeClass):
    @contextlib.contextmanager
    def on_connection(self, target_terminal, source_terminal):
        with super().on_connection(target_terminal, source_terminal):
            ...
            try:
                yield
            finally:
                ...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top