给出一个班级
class SomeClass:
@contextlib.contextmanager
def on_connection(self, target_terminal, source_terminal):
...
yield
...
你如何从中继承?
答案 0 :(得分:2)
可以将contextlib.contextmanager
模式与with
块组合在一起,以引入超类'上下文管理器:
class SomeDerivedClass(SomeClass):
@contextlib.contextmanager
def on_connection(self, target_terminal, source_terminal):
with super().on_connection(target_terminal, source_terminal):
...
try:
yield
finally:
...