【发布时间】:2020-04-27 10:56:59
【问题描述】:
假设我有:
class Super:
def __init__(self,a):
self.a = a
@classmethod
def from_b(cls,b):
return cls(b.to_a())
class Regular(Super):
def __init__(self,b):
# how to set my super to the output of
super = super.from_b(b)
如何使用超类方法的输出而不是 init 来正确初始化超类?
我的 OOP 背景是 C++,由于能够在 C++ 中重载构造函数,我不断地进入这些场景,所以解决这个问题会很棒。
【问题讨论】:
-
我认为最自然的做法是
class Regular(Super): pass,然后使用现有接口从bs 创建实例:Regular.from_b(b)
标签: python python-3.x inheritance constructor super