我调用祖先函数 init ,但这没有调用祖先函数 set_param(),它调用了descadent函数。在我的脚本中,这意味着父亲变量 self.a 未初始化。你能跟我解释一下吗?谢谢。
class father (object):
def __init__(self):
self.set_param()
def set_param(self):
self.a = 'father' # isn't initialized
class son (father):
def __init__(self):
father.__init__(self)
self.set_param()
def set_param(self):
self.b = 'son'
person = son()
答案 0 :(得分:1)
self.set_param()
被两次调用,分别来自son.__init__
和father.__init__
,但两次都被调用son.set_param
,因为两者中的type(self)
是son
案件。从未调用father.set_param
。