这样写为什么出错
这样写为什么出错了
class myclass(object): def __init__(self, name): self.name = name class yourclass(object): def __init__(self, age): self.age = age class A(myclass, yourclass): def __init__(self, name, age): myclass.__init__(self, name) yourclass.__init__(self, age) a = A('ze', 18) print a.name print a.age
用super来找父类的时候,会遵循一定的顺序,经典的是从左至右,深度搜索,所以在例子里永远都是找到了myclass里的init方法。建议用上面的 ClassName.__init__(self, args)。。下面的也可以:
class myclass(object): def __init__(self, name): self.name = name class yourclass(object): def __init__(self, age): self.age = age class A(myclass, yourclass): def __init__(self, name, age): super(A, self).__init__(name) yourclass.__init__(self, age) a = A('ze', 18) print a.name print a.age
举报