Python ChildClass对象没有属性栏

Python ChildClass对象没有属性栏,python,python-2.7,oop,inheritance,multiple-inheritance,Python,Python 2.7,Oop,Inheritance,Multiple Inheritance,这是我的类的结构,Foo类继承了Baz类。但是,当我尝试运行代码时,我得到以下错误 Foo对象没有属性栏 据我所知,super(childclass,self).\uuu init\uu(parent,attributes)也应该初始化父类。为什么bar也应该出现在子类中 我遵循以下堆栈答案来实现这一点: 编辑 class Foo(Baz, Qux): def __init__(self, bar, *args, **kwargs): super(Foo, self)._

这是我的类的结构,Foo类继承了Baz类。但是,当我尝试运行代码时,我得到以下错误
Foo对象没有属性栏

据我所知,
super(childclass,self).\uuu init\uu(parent,attributes)
也应该初始化父类。为什么
bar
也应该出现在子类中

我遵循以下堆栈答案来实现这一点:

编辑

class Foo(Baz, Qux):
    def __init__(self, bar, *args, **kwargs):
        super(Foo, self).__init__(bar=bar, *args, **kwargs)

class Baz:   
    def __init__(self, bar, *args, **kwargs):
        self.bar = bar
输出:

class Qux:   
    def __init__(self, xyz, *args, **kwargs):
        self.xyz = xyz
    
    def print_barqux(self):
        print(self.xyz)
        
class Baz:   
    def __init__(self, bar, *args, **kwargs):
        self.bar = bar
        
class Foo(Baz, Qux):
    def __init__(self, bar, xyz, *args, **kwargs):
        super(Foo, self).__init__(bar=bar, xyz=xyz)

    def printbar(self):
        super(Foo, self).print_barqux()
        
        
foo = Foo(bar="abcd", xyz="abcdef")
print(foo.printbar())
回溯(最近一次呼叫最后一次):
文件“/prog.py”,第21行,在
文件“/prog.py”,第17行,在打印栏中
文件“/prog.py”,第6行,打印
AttributeError:“Foo”对象没有属性“xyz”

使用代码添加错误stacktrace

每个类都必须调用
super.\uuuu init\uuuu

Traceback (most recent call last):
  File "./prog.py", line 21, in <module>
  File "./prog.py", line 17, in printbar
  File "./prog.py", line 6, in print_barqux
AttributeError: 'Foo' object has no attribute 'xyz'

每个类都必须调用
super.\uuu init\uuu

Traceback (most recent call last):
  File "./prog.py", line 21, in <module>
  File "./prog.py", line 17, in printbar
  File "./prog.py", line 6, in print_barqux
AttributeError: 'Foo' object has no attribute 'xyz'

订购有关系吗?见鬼,是的
Baz
必须完全定义,然后才能将其引用为
Foo
的基类。如果您有这样的多重继承,那么
super
可能不是正确的答案。您可以通过执行
Baz.\uuuuu init\uuuuu(self,bar=bar,*args,**kwargs)
@TimRoberts来专门针对
Baz
super
的全部目的是帮助协作多重继承。super调用MRO中的下一个类,它可能是父类,也可能是子类的另一个父类。在这种情况下,在
Qux
中调用
super()
Baz
@SwastikUdupa no
super
中调用
初始化
调用方法解析顺序中的下一个类排序是否重要?见鬼,是的
Baz
必须完全定义,然后才能将其引用为
Foo
的基类。如果您有这样的多重继承,那么
super
可能不是正确的答案。您可以通过执行
Baz.\uuuuu init\uuuuu(self,bar=bar,*args,**kwargs)
@TimRoberts来专门针对
Baz
super
的全部目的是帮助协作多重继承。super调用MRO中的下一个类,它可能是父类,也可能是子类的另一个父类。在这种情况下,在
Qux
中调用
super()
Baz
@SwastikUdupa no
super
中调用
\uuu init\uuuu
调用方法解析顺序中的下一个类