相关疑难解决方法(0)

为什么这是一个模棱两可的MRO?

class First(object):
    def __init__(self):
        print "first"

class Second(First):
    def __init__(self):
        print "second"

class Third(First, Second):
    def __init__(self):
        print "third"
Run Code Online (Sandbox Code Playgroud)

资源

为什么Python不能创建一致的MRO?在我看来它非常清楚:

  1. 如果第三个方法不存在,则首先搜索
  2. 如果在First中不存在方法,则在第二个中搜索

但如果你尝试一下:

TypeError: Error when calling the metaclass bases
    Cannot create a consistent method resolution
order (MRO) for bases First, Second
Run Code Online (Sandbox Code Playgroud)

python multiple-inheritance

11
推荐指数
2
解决办法
1241
查看次数

在从`object`派生的类中调用super().__ init __()?

Python 文档__init__每个类的方法负责初始化它的超类.但对于新式课程,最终的基础是object.做dir(object)一些object自己有__init__方法并可能被初始化的节目.有什么理由这样做吗?

我倾向于这样做以保持一致性和(略微)更容易重构类heirarchy,但我想知道它是否是严格必要的或被认为是最佳实践.

python

10
推荐指数
3
解决办法
1691
查看次数

Python多重继承:在所有上调用super

我有以下两个超类:

class Parent1(object):
    def on_start(self):
        print('do something')

class Parent2(object):
    def on_start(self):
        print('do something else')
Run Code Online (Sandbox Code Playgroud)

我希望有一个继承的子类可以为父母双方打电话.

class Child(Parent1, Parent2):
    def on_start(self):
        # super call on both parents
Run Code Online (Sandbox Code Playgroud)

什么是Pythonic的方法呢?谢谢.

python

10
推荐指数
1
解决办法
3562
查看次数

为什么在多继承而不是super().__ init __()中执行Base .__ init __(self)时会跳过__init__?

究竟是为什么

A.__init__()
B.__init__()
D.__init__()
Run Code Online (Sandbox Code Playgroud)

由以下代码打印?特别是:

  1. 为什么C.__init__() 打印?

  2. 为什么C.__init__()打印super().__init__()而不是A.__init__(self)

#!/usr/bin/env python3

class A(object):
    def __init__(self):
        super(A, self).__init__()
        print("A.__init__()")
class B(A):
    def __init__(self):
        A.__init__(self)
        print("B.__init__()")
class C(A):
    def __init__(self):
        A.__init__(self)
        print("C.__init__()")
class D(B, C):
    def __init__(self):
        super(D, self).__init__()
        print("D.__init__()")

D()
Run Code Online (Sandbox Code Playgroud)

python multiple-inheritance super diamond-problem python-3.x

10
推荐指数
1
解决办法
119
查看次数

如何创建具有多重继承的类

我有这个代码:

class Person:
    def __init__(self, name, last_name, age):
        self.name = name
        self.last_name = last_name
        self.age = age 

class Student(Person):
    def __init__(self, name, last_name, age, indexNr, notes):
        super().__init__(name, last_name, age)
        self.indexNr = indexNr
        self.notes = notes

class Employee(Person):
    def __init__(self, name, last_name, age, salary, position):
        super().__init__(name, last_name, age)
        self.salary = salary
        self.position = position

class WorkingStudent(Student, Employee):
    def __init__(self, name, last_name, age, indexNr, notes, salary, position):
        Student.__init__(name, last_name, age, indexNr, notes)
        Employee.__init__(name, last_name, age, salary, position)
Run Code Online (Sandbox Code Playgroud)

我想创建一个像这样的WorkingStudent实例:

ws = WorkingStudent("john", "brown", …
Run Code Online (Sandbox Code Playgroud)

python oop multiple-inheritance python-3.x

9
推荐指数
1
解决办法
687
查看次数

__init__是一种类方法吗?

我正在研究Python的超级方法和多重继承.我读到的东西就像当我们使用super来调用一个在所有基类中都有实现的基本方法时,即使有各种参数,也只会调用一个类的方法.例如,

class Base1(object):
    def __init__(self, a):
        print "In Base 1"

class Base2(object):
    def __init__(self):
        print "In Base 2"

class Child(Base1, Base2):
    def __init__(self):
        super(Child, self).__init__('Intended for base 1')
        super(Child, self).__init__()# Intended for base 2
Run Code Online (Sandbox Code Playgroud)

这产生TyepError了第super一种方法.super会调用它首先识别的方法实现,TypeError而不是检查其他类.但是,当我们执行以下操作时,这将更加清晰并且正常工作:

class Child(Base1, Base2):
    def __init__(self):
        Base1.__init__(self, 'Intended for base 1')
        Base2.__init__(self) # Intended for base 2
Run Code Online (Sandbox Code Playgroud)

这导致两个问题:

  1. __init__方法的静态方法或类方法?
  2. 为什么要使用super,它隐式选择自己的方法,而不是像后一个例子那样显式调用方法?它看起来比使用super更清洁.那么使用super第二种方式的优点是什么(除了使用方法调用编写基类名称)

python inheritance multiple-inheritance python-2.7

8
推荐指数
2
解决办法
2113
查看次数

在多重继承中访问超类方法的更好方法

class Animal(object):
    def eat(self):
        print("I eat all")

class C(object):
    def eat(self):
        print("I too eat")

class Wolf(C, Animal):
    def eat(self):
        print("I am Non Veg")
        super(Wolf, self).eat()
        Animal.eat(self)

w = Wolf()
w.eat()
Run Code Online (Sandbox Code Playgroud)

我正在学习python中的多重继承,我想使用方法从派生类访问Animal和方法.Ceatsuper

super内部调用C 类方法的默认调用eat,但调用Animal我使用的类方法.Animal.eat(self)我的问题是如何Animal使用super方法调用类方法.

python

7
推荐指数
1
解决办法
9148
查看次数

当直接从`object`继承时,我应该调用super().__ init __()吗?

因为这个问题是关于继承的super,所以我们先写一个类.这是一个代表一个人的简单日常课程:

class Person:
    def __init__(self, name):
        super().__init__()

        self.name = name
Run Code Online (Sandbox Code Playgroud)

像每个好类一样,它在初始化之前调用它的父构造函数.这个班级完美地完成了它的工作; 它可以毫无问题地使用:

>>> Person('Tom')
<__main__.Person object at 0x7f34eb54bf60>
Run Code Online (Sandbox Code Playgroud)

但是当我尝试创建一个继承自两个Person类和另一个类的类时,事情突然出现了:

class Horse:
    def __init__(self, fur_color):
        super().__init__()

        self.fur_color = fur_color

class Centaur(Person, Horse):
    def __init__(self, name, fur_color):
        # ??? now what?
        super().__init__(name)  # throws TypeError: __init__() missing 1 required positional argument: 'fur_color'
        Person.__init__(self, name)  # throws the same error
Run Code Online (Sandbox Code Playgroud)

由于钻石继承(object顶部的类),因此无法Centaur正确初始化实例.将super().__init__()Person结束了通话Horse.__init__,因为它抛出一个异常fur_color参数丢失.

但是如果PersonHorse不打电话,这个问题就不会存在super().__init__() …

python inheritance super

7
推荐指数
2
解决办法
755
查看次数

TypeError:具有不同参数的python多重继承

我正在尝试使用多重继承向我拥有的现有类之一添加一些功能。问题是这个新类和我当前的基类在它们的构造函数中有不同的参数。即新类有 1 个额外的参数。经过一番谷歌搜索,我明白我可以将 **kwargs 添加到当前基类(少一个参数的基类)。例子:

class A(object):
    def __init__(self, a):
        print('__init__', locals())


class B(A):
    def __init__(self, a, b):
        super(B, self).__init__(a)
        print('__init__', locals())


class C(B):
    def __init__(self, a, b):
        super(C, self).__init__(a, b)
        print('__init__', locals())


class D(C):
    def __init__(self, a, b):
        super(D, self).__init__(a, b)
        print('__init__', locals())


class E(D):
    def __init__(self, a, b, *args, **kwargs):
        super(E, self).__init__(a, b)
        print('__init__', locals())


class F(C):
    def __init__(self, a, b):
        super(F, self).__init__(a, b)
        print('__init__', locals())


class G(F):
    def __init__(self, a, b, c):
        super(G, self).__init__(a, b)
        print('__init__', locals()) …
Run Code Online (Sandbox Code Playgroud)

python multiple-inheritance

7
推荐指数
1
解决办法
230
查看次数

如何在使用多重继承时使用super初始化所有父项

Python的super()如何与多重继承一起工作?

我正在看上面的问题/答案,让自己很困惑

 53 class First(object):
 54     def __init__(self):
 55         print "first"
 56
 57 class Second(First):
 58     def __init__(self):
 59         super(Second, self).__init__()
 60         print "second"
 61
 62 class Third(First):
 63     def __init__(self):
 64         print "third"
 65
 66 class Fourth(Second, Third):
 67     def __init__(self):
 68         super(Fourth, self).__init__()
 69         print "thats it"
Run Code Online (Sandbox Code Playgroud)

第四()
第三

就是这样

 53 class First(object):
 54     def __init__(self):
 55         print "first"
 56
 57 class Second(First):
 58     def __init__(self):
 59         #super(Second, self).__init__()         <---- commented out
 60         print "second"
 61
 62 …
Run Code Online (Sandbox Code Playgroud)

python

6
推荐指数
1
解决办法
139
查看次数