python类中super()和

单继承时super()和__init__()实现的功能是类似的

class base(object):
def __init__(self):
print ‘base create’
class childa(base):
def __init__(self):
print ‘creat a ‘,
base.__init__(self)
class childb(base):
def __init__(self):
print ‘creat b ‘,
super(childb, self).__init__()
base = base()
a = childa()
b = childb()

输出结果:

base create
creat a base create
creat b base create

区别是使用super()继承时不用显式引用基类。

super()只能用于新式类中

把基类改为旧式类,即不继承任何基类

class base():
def __init__(self):
print ‘base create’

执行时,在初始化b时就会报错:

super(childb, self).__init__()
typeerror: must be type, not classobj

super不是父类,而是继承顺序的下一个类

在多重继承时会涉及继承顺序,super()相当于返回继承顺序的下一个类,而不是父类,类似于这样的功能:

def super(class_name, self):
mro = self.__class__.mro()
return mro[mro.index(class_name) + 1]

mro()用来获得类的继承顺序。

例如:

class base(object):
def __init__(self):
print ‘base create’
class childa(base):
def __init__(self):
print ‘enter a ‘
# base.__init__(self)
super(childa, self).__init__()
print ‘leave a’
class childb(base):
def __init__(self):
print ‘enter b ‘
# base.__init__(self)
super(childb, self).__init__()
print ‘leave b’
class childc(childa, childb):
pass
c = childc()
print c.__class__.__mro__

输出结果如下:

enter a
enter b
base create
leave b
leave a
(, , , , )

supder和父类没有关联,因此执行顺序是a —> b—>—>base

执行过程相当于:初始化childc()时,先会去调用childa的构造方法中的 super(childa, self).__init__(), super(childa, self)返回当前类的继承顺序中childa后的一个类childb;然后再执行childb().__init()__,这样顺序执行下去。

在多重继承里,如果把childa()中的 super(childa, self).__init__() 换成base.__init__(self),在执行时,继承childa后就会直接跳到base类里,而略过了childb:

enter a
base create
leave a
(, , , , )

从super()方法可以看出,super()的第一个参数可以是继承链中任意一个类的名字,

如果是本身就会依次继承下一个类;

如果是继承链里之前的类便会无限递归下去;

如果是继承链里之后的类便会忽略继承链汇总本身和传入类之间的类;

比如将childa()中的super改为:super(childc, self).init(),程序就会无限递归下去。

如:

file “c:/users/administrator/desktop/crawler/learn.py”, line 10, in __init__
super(childc, self).__init__()
file “c:/users/administrator/desktop/crawler/learn.py”, line 10, in __init__
super(childc, self).__init__()
file “c:/users/administrator/desktop/crawler/learn.py”, line 10, in __init__
super(childc, self).__init__()
file “c:/users/administrator/desktop/crawler/learn.py”, line 10, in __init__
super(childc, self).__init__()
file “c:/users/administrator/desktop/crawler/learn.py”, line 10, in __init__
super(childc, self).__init__()
file “c:/users/administrator/desktop/crawler/learn.py”, line 10, in __init__
super(childc, self).__init__()
file “c:/users/administrator/desktop/crawler/learn.py”, line 10, in __init__
super(childc, self).__init__()
file “c:/users/administrator/desktop/crawler/learn.py”, line 10, in __init__
super(childc, self).__init__()
file “c:/users/administrator/desktop/crawler/learn.py”, line 10, in __init__
super(childc, self).__init__()
file “c:/users/administrator/desktop/crawler/learn.py”, line 10, in __init__
super(childc, self).__init__()
file “c:/users/administrator/desktop/crawler/learn.py”, line 10, in __init__
super(childc, self).__init__()
file “c:/users/administrator/desktop/crawler/learn.py”, line 10, in __init__
super(childc, self).__init__()
file “c:/users/administrator/desktop/crawler/learn.py”, line 10, in __init__
super(childc, self).__init__()
runtimeerror: maximum recursion depth exceeded while calling a python object

super()可以避免重复调用

如果childa基础base, childb继承childa和base,如果childb需要调用base的__init__()方法时,就会导致__init__()被执行两次:

class base(object):
def __init__(self):
print ‘base create’
class childa(base):
def __init__(self):
print ‘enter a ‘
base.__init__(self)
print ‘leave a’
class childb(childa, base):
def __init__(self):
childa.__init__(self)
base.__init__(self)
b = childb()

base的__init__()方法被执行了两次

enter a
base create
leave a
base create

使用super()是可避免重复调用

class base(object):
def __init__(self):
print ‘base create’
class childa(base):
def __init__(self):
print ‘enter a ‘
super(childa, self).__init__()
print ‘leave a’
class childb(childa, base):
def __init__(self):
super(childb, self).__init__()
b = childb()
print b.__class__.mro()
enter a
base create
leave a
[, , , ]

以上所述是小编给大家介绍的python类中super()和__init__()的区别,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

Posted in 未分类

发表评论