理解Python中super

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. 单继承

在单继承时,super().__init__()Base.__init__()是一样的。super()避免了基类的显式调用。代码如下:

  • super().init()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Base(object):
def __init__(self):
print 'Create Base'

class A(Base):
def __init__(self):
Base.__init__(self)
print 'Create A'

A()

# 测试结果
Create Base
Create A
  • Base.init()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Base(object):
def __init__(self):
print 'Create Base'

class A(Base):
def __init__(self):
super(A, self).__init__()
# super().__init() python3
print 'Create A'

A()

# 测试结果
Create Base
Create A

2. 多继承

super与父类没有实质性的关联。在单继承时,super获取的类刚好是父类,但多继承时,super获取的是继承顺序中的下一个类。以下面的继承方式为例:

1
2
3
4
5
6
7
  Base
/ \
/ \
A B
\ /
\ /
C

使用super,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Base(object):
def __init__(self):
print "enter Base"
print "leave Base"

class A(Base):
def __init__(self):
print "enter A"
super(A, self).__init__()
print "leave A"

class B(Base):
def __init__(self):
print "enter B"
super(B, self).__init__()
print "leave B"

class C(A, B):
def __init__(self):
print "enter C"
super(C, self).__init__()
print "leave C"

C()

# 测试结果
enter C
enter A
enter B
enter Base
leave Base
leave B
leave A
leave C

如果不使用super,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Base(object):
def __init__(self):
print "enter Base"
print "leave Base"

class A(Base):
def __init__(self):
print "enter A"
Base().__init__()
print "leave A"

class B(Base):
def __init__(self):
print "enter B"
Base().__init__()
print "leave B"

class C(A, B):
def __init__(self):
print "enter C"
A().__init__()
B().__init__()
print "leave C"

C()

# 测试结果
enter C
enter A
enter Base
leave Base
enter Base
leave Base
leave A
enter A
enter Base
leave Base
enter Base
leave Base
leave A
enter B
enter Base
leave Base
enter Base
leave Base
leave B
enter B
enter Base
leave Base
enter Base
leave Base
leave B
leave C

从上面可以看到如果不使用super,会导致基类被多次调用,开销非常大。

对于定义的类,在Python中会创建一个MRO(Method Resolution Order)列表,它代表了类继承的顺序。查看MRO列表:

  • 使用super
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Base(object):
def __init__(self):
print "Create Base"

class A(Base):
def __init__(self):
super(A, self).__init__()
print "Create A"

class B(Base):
def __init__(self):
super(B, self).__init__()
print "Create B"

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

print C.mro()

# 测试结果

[<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class '__main__.Base'>, <type 'object'>]
  • 不使用super
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Base(object):
def __init__(self):
print "enter Base"
print "leave Base"

class A(Base):
def __init__(self):
print "enter A"
Base().__init__()
print "leave A"

class B(Base):
def __init__(self):
print "enter B"
Base().__init__()
print "leave B"

class C(A, B):
def __init__(self):
print "enter C"
A().__init__()
B().__init__()
print "leave C"

print C.mro()

# 测试结果
[<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class '__main__.Base'>, <type 'object'>]

从测试结果来看,两种方式的MRO列表是一样的。MRO的查找顺序是按广度优先来的(基类继承object,Python 2.3之后)。

参考资料

  1. https://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods
  2. https://my.oschina.net/jhao104/blog/682322
  3. http://funhacks.net/explore-python/Class/super.html
  4. https://www.artima.com/weblogs/viewpost.jsp?thread=236275
  5. https://docs.python.org/2/library/functions.html#super
  6. https://blog.51cto.com/freshair/2063290
如果有收获,可以请我喝杯咖啡!