我需要从第三方模块m的类X中获取一些功能。我可以直接使用m.X,但是将来可能需要用另一个类n.Y替换m.X(例如,如果我发现更好的实现)。
在这种情况下,我希望避免改变其余代码。目前,我想要通过以下方式编写一个包装器W,以便完全保留m.X的接口,包括初始化:
class W(m.X):
def __init__(self, *args):
super().__init__(*args)
在未来,如果需要的话,我计划将上述内容重写为:
class W(n.Y):
def __init__(self, *args):
super().__init__(*args)
# override instance methods of n.Y that don't share the semantics with m.X
# for example, in case f1 is hard to replicate in n.Y:
# def f1(self, *args):
# print("this method is no longer available")
# raise MyDeprecatedMethod()
# for example, in case f2 needs to be recalculated
# def f2(self, *args):
# do the calculations required to keep W.f2 unchanged
我的m.X包装器目前可用吗?它有问题吗,或者计划中的n.Y包装器有问题吗?