Python多重继承,从公共(public)子类初始化

标签 python

如何让一个公共(public)子类初始化它继承自的所有父类?

class Mom(object):
    def __init__(self):
        print("Mom")

class Dad(object):
    def __init__(self):
        print("Dad")

class Child(Mom, Dad):
    def __init__(self):
        super(Child, self).__init__()

c = Child() #only prints Mom

最佳答案

它们缺少 MomDad 类中的 super() 调用,这是合作子类化工作所必需的。

class Mom(object):
    def __init__(self):
        super(Mom, self).__init__()
        print("Mom")

class Dad(object):
    def __init__(self):
        super(Dad, self).__init__()
        print("Dad")

class Child(Dad, Mom):
    def __init__(self):
        super(Child, self).__init__()
c = Child() # Mom, Dad

关于Python多重继承,从公共(public)子类初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42229222/

相关文章:

python - Pandas DataFrame 迭代切片

python - Xpath 用于使用类查找 anchor 标记内的文本(Scrapy)

python - 获取二维数组中连续区域边界的路径

php - PHP 是否具有与这种类型的 Python 字符串替换等效的功能?

python - "Settings"用于 Python 中的函数

python - 使用 block 切割 DNA 序列

Python:使用 pandas 从 Excel 转换为 CSV 时保留前导零

python - django:如何在模板 html 页面内进行计算?

python - 类没有属性 'tk'

python - RDF 的模板系统?