多重继承 Python, super().__init__ 问题

Multiple inheritance Python, super().__init__ issue

我是 Python 的新手,我正在尝试了解多重继承。 下面我有一段代码,我需要在其中创建一个 Animals.

数组

这里是 类:

class Animal:
    def __init__(self, birthDate, type, name, numOfLegs):
        self.birthDate = birthDate
        self.type = type
        self.name = name
        self.numOfLegs = numOfLegs


class Domesticated(Animal):
    def __init__(self, birthDate, type, name, numOfLegs, lastVetCheck):
        super().__init__(birthDate, type, name, numOfLegs)
        self.lastVetCheck = lastVetCheck


class Feline(Animal):
    def __init__(self, birthDate, type, name, numOfLegs, mustacheLength):
        super().__init__(birthDate, type, name, numOfLegs)
        self.mustacheLength = mustacheLength


class Cat(Feline, Domesticated):
    def __init__(self, mustacheLength, numOfLegs, name, type, bDate, vetDate):
        Feline.__init__(self, bDate, type, name, numOfLegs, mustacheLength)
        Domesticated.__init__(self,bDate, type, name, numOfLegs, vetDate)


class Tiger(Feline):
    def __init__(self, birthDate, type, name, numOfLegs, mustacheLength):
        super().__init__(birthDate, type, name, numOfLegs, mustacheLength)

代码重述:

animal_array = []
my_cat = Cat('4', '4', 'Tom', 'Mammal', '1.2.3', '3.4.5')
my_animal = Animal('6.7.8', 'Reptile', 'Rafael', '4')
my_tiger = Tiger('1.1.1', 'Mammal', 'Tiger', '4', '9')
animal_array.append(my_cat)
animal_array.append(my_animal)
animal_array.append(my_tiger)

我收到此错误:

Traceback (most recent call last):

  File "C:/Users/Name/PycharmProjects/Pandas/test.py", line 33, in <module>
    my_cat = Cat('4', '4', 'abc', 'mammal', '1.2.3', '3.4.5')

  File "C:/Users/Name/PycharmProjects/Pandas/test.py", line 23, in __init__
    Feline.__init__(self, bDate, type, name, numOfLegs, mustacheLength)

  File "C:/Users/Name/PycharmProjects/Pandas/test.py", line 17, in __init__
    super().__init__(birthDate, type, name, numOfLegs)

TypeError: __init__() missing 1 required positional argument: 'lastVetCheck'

现在我确定这是一个简单的问题,可能是我的 类 结构有问题,但我不知道要解决什么问题。我肯定可以使用任何结构化技巧,因为我是 OO 的新手。

不幸的是,当您使用多重继承并尝试向每个父亲发送不同的参数时,您不能像那样使用 super() class。

如果要保留 classes 的结构,则需要按如下方式更改代码:

class Animal:
    def __init__(self, birthDate, type, name, numOfLegs):
        self.birthDate = birthDate
        self.type = type
        self.name = name
        self.numOfLegs = numOfLegs


class Domesticated(Animal):
    def __init__(self, birthDate, type, name, numOfLegs, lastVetCheck):
        Animal.__init__(self, birthDate, type, name, numOfLegs)
        self.lastVetCheck = lastVetCheck


class Feline(Animal):
    def __init__(self, birthDate, type, name, numOfLegs, mustacheLength):
        Animal.__init__(self, birthDate, type, name, numOfLegs)
        self.mustacheLength = mustacheLength


class Cat(Feline, Domesticated):
    def __init__(self, mustacheLength, numOfLegs, name, type, bDate, vetDate):
        Feline.__init__(self, bDate, type, name, numOfLegs, mustacheLength)
        Domesticated.__init__(self,bDate, type, name, numOfLegs, vetDate)


class Tiger(Feline):
    def __init__(self, birthDate, type, name, numOfLegs, mustacheLength):
        super().__init__(birthDate, type, name, numOfLegs, mustacheLength)

示例:

my_cat = Cat('4', '4', 'Tom', 'Mammal', '1.2.3', '3.4.5')
print(my_cat.name)

输出:

Tom

如果您设计 classes 并正确使用 super,这不是问题。 Every class(甚至 Animal)应该接受任意关键字参数以传递给 super().__init__。每个 class 列出了它将处理的参数名称,而无需明确地向上游传递。当您实例化一个对象时, 使用关键字参数,您可以在 Python 3 中通过将命名参数设为仅关键字来强制执行。

如果您正确定义了每个 __init__,那么在 object.__init__super.[=20 的调用之一调用时 kwargs 应该是空的=]

class Animal:
    def __init__(self, *, birthDate, type, name, numOfLegs, **kwargs):
        super().__init__(**kwargs)
        self.birthDate = birthDate
        self.type = type
        self.name = name
        self.numOfLegs = numOfLegs


class Domesticated(Animal):
    def __init__(self, *, lastVetCheck, **kwargs):
        super().__init__(**kwargs)
        self.lastVetCheck = lastVetCheck


class Feline(Animal):
    def __init__(self, *, mustacheLength, **kwargs):
        super().__init__(**kwargs)
        self.mustacheLength = mustacheLength


class Cat(Feline, Domesticated):
    pass


class Tiger(Feline):
    pass



animals = [
    Cat(mustacheLength='4', numOfLegs='4', name='Tom', type='Mammal', bDate='1.2.3', vetDate'3.4.5')
    Animal(birthDate='6.7.8', type='Reptile', name='Rafael', numOfLegs'4')
    Tiger(bDate='1.1.1', type='Mammal', name='Tiger', numLegs='4', mustacheLength='9')
]

注意没有重复;在任何子 classes 中都没有重复所有动物共有的东西。唯一的样板是在每个 class 的 __init__ 方法中对 super().__init__(**kwargs) 的相同调用。