相关疑难解决方法(0)

使用__init __()方法理解Python super()

我正在努力了解它的用法super().从它的外观来看,可以创建两个子类,就好了.

我很想知道以下2个孩子班级之间的实际差异.

class Base(object):
    def __init__(self):
        print "Base created"

class ChildA(Base):
    def __init__(self):
        Base.__init__(self)

class ChildB(Base):
    def __init__(self):
        super(ChildB, self).__init__()

ChildA() 
ChildB()
Run Code Online (Sandbox Code Playgroud)

python oop inheritance class super

2366
推荐指数
7
解决办法
158万
查看次数

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

我是Python面向对象编程的新手,我很难理解super()函数(新样式类),尤其是涉及多重继承时.

例如,如果你有类似的东西:

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

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

class Third(First, Second):
    def __init__(self):
        super(Third, self).__init__()
        print "that's it"
Run Code Online (Sandbox Code Playgroud)

我没有得到的是:Third()该类是否会继承构造函数方法?如果是,那么将使用super()运行哪一个?为什么?

如果你想运行另一个怎么办?我知道它与Python方法解析顺序(MRO)有关.

python multiple-inheritance

825
推荐指数
14
解决办法
30万
查看次数

Python如何获取实例的基本实例?

在C#我会去:

myObj.base
Run Code Online (Sandbox Code Playgroud)

我有一个继承自date.datetime的Date类.Date类会覆盖__gt__(),__lt__()因此在使用<>运算符时会调用它们.我不想使用这些覆盖 - 我想在Date实例上使用date.datetime方法.

python python-2.6

4
推荐指数
1
解决办法
4312
查看次数