首页 后端开发 Python教程 深入解析Python编程中super关键字的用法

深入解析Python编程中super关键字的用法

Jul 06, 2016 pm 01:29 PM
python super

官方文档中关于super的定义说的不是很多,大致意思是返回一个代理对象让你能够调用一些继承过来的方法,查找的机制遵循mro规则,最常用的情况如下面这个例子所示:

1

2

3

class C(B):

  def method(self, arg):

    super(C, self).method(arg)

登录后复制

子类C重写了父类B中同名方法method,在重写的实现中通过super实例化的代理对象调用父类的同名方法。

super类的初始方法签名如下:

1

2

3

4

5

6

def __init__(self, type1, type2=None): # known special case of super.__init__

    """

    super(type, obj) -> bound super object; requires isinstance(obj, type)

    super(type) -> unbound super object

    super(type, type2) -> bound super object; requires issubclass(type2, type)

    Typical use to call a cooperative superclass method:

登录后复制

除去self外接受一个或者或者两个参数,如同注释声明的一样,接受两个参数时返回的是绑定的super实例,省略第二个参数的时候返回的是未绑定的super对象。

一般情况下当调用继承的类方法或者静态方法时,并不需要绑定具体的实例,这个时候使用super(type, type2).some_method就能达到目的,当然super(type, obj)在这种情况下也能够使用,super对象有自定义实现的getattribute方法也能够处理。不过,后者一般用来调用实例方法,这样在查找方法的时候能够传入相应的实例,从而得到绑定的实例方法:

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

class A(object):

  def __init__(self):

    pass

 

  @classmethod

  def klass_meth(cls):

    pass

 

  @staticmethod

  def static_meth():

    pass

 

  def test(self):

    pass

 

class B(A):

  pass

 

>>> b = B()

>>> super(B, b).test

<bound method B.test of <__main__.B object at 0x02DA3570>>

>>> super(B, b).klass_meth

<bound method type.klass_meth of <class '__main__.B'>>

>>> super(B, b).static_meth

<function static_meth at 0x02D9CC70>

>>> super(B, B).test

<unbound method B.test>

>>> super(B, B).klass_meth

<bound method type.klass_meth of <class '__main__.B'>>

>>> super(B,B).satic_meth

>>> super(B,B).static_meth

<function static_meth at 0x02D9CC70>

登录后复制

初始化super对象的时候,传递的第二个参数其实是绑定的对象,第一个参感觉数可以粗暴地理解为标记查找的起点,比如上面例子中的情况:super(B, b).test就会在B.__mro__里面列出的除B本身的类中查找方法test,因为方法都是非数据描述符,在super对象的自定义getattribute里面实际上会转化成A.__dict['test'].__get__(b, B)。

super在很多地方都会用到,除了让程序不必hardcode指定类型让代码更加动态,还有其他一些具体必用的地方比如元类中使用super查找baseclass里面的new生成自定义的类型模板;在自定义getattribute的时候用来防止无限循环等等。

关于super建议读者将它与python的描述符一起来理解,因为super就实现了描述符的协议,是一个非数据描述符,能够帮助大家更好的理解super的使用和工作原理。

同时,有以下4个点值得大家注意:
1、单继承时super()和__init__()实现的功能是类似的

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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()

登录后复制

输出结果:

1

2

3

Base create

creat A Base create

creat B Base create

登录后复制


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

2、super()只能用于新式类中

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

1

2

3

class Base():

  def __init__(self):

    print 'Base create'

登录后复制

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

1

2

  super(childB, self).__init__()

TypeError: must be type, not classobj

登录后复制

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

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

1

2

3

def super(class_name, self):

  mro = self.__class__.mro()

  return mro[mro.index(class_name) + 1]

登录后复制

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

例如:

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 '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__

登录后复制

输入结果如下:

1

2

3

4

5

6

enter A

enter B

Base create

leave B

leave A

(<class '__main__.childC'>, <class '__main__.childA'>, <class '__main__.childB'>, <class '__main__.Base'>, <type 'object'>)

登录后复制

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:

1

2

3

4

enter A

Base create

leave A

(<class '__main__.childC'>, <class '__main__.childA'>, <class '__main__.childB'>, <class '__main__.Base'>, <type 'object'>)

登录后复制

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

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

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

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

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

如:

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

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

登录后复制

4、super()可以避免重复调用

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

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

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()

登录后复制

1

2

3

4

enter A

Base create

leave A

[<class '__main__.childB'>, <class '__main__.childA'>, <class '__main__.Base'>, <type 'object'>]

登录后复制
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

如何在LAMP架构下高效整合Node.js或Python服务? 如何在LAMP架构下高效整合Node.js或Python服务? Apr 01, 2025 pm 02:48 PM

在LAMP架构下整合Node.js或Python服务许多网站开发者都面临这样的问题:已有的LAMP(Linux Apache MySQL PHP)架构网站需要...

如何解决Linux终端中查看Python版本时遇到的权限问题? 如何解决Linux终端中查看Python版本时遇到的权限问题? Apr 01, 2025 pm 05:09 PM

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

使用Scapy爬虫时,管道持久化存储文件无法写入的原因是什么? 使用Scapy爬虫时,管道持久化存储文件无法写入的原因是什么? Apr 01, 2025 pm 04:03 PM

使用Scapy爬虫时,管道持久化存储文件无法写入的原因探讨在学习使用Scapy爬虫进行数据抓取时,经常会遇到一�...

Python进程池处理并发TCP请求导致客户端卡死的原因是什么? Python进程池处理并发TCP请求导致客户端卡死的原因是什么? Apr 01, 2025 pm 04:09 PM

Python进程池处理并发TCP请求导致客户端卡死的解析在使用Python进行网络编程时,高效处理并发TCP请求至关重要。...

如何查看Python functools.partial对象内部封装的原始函数? 如何查看Python functools.partial对象内部封装的原始函数? Apr 01, 2025 pm 04:15 PM

深入探讨Pythonfunctools.partial对象的查看方法在使用Python的functools.partial...

Python跨平台桌面应用开发:哪个GUI库最适合你? Python跨平台桌面应用开发:哪个GUI库最适合你? Apr 01, 2025 pm 05:24 PM

Python跨平台桌面应用开发库的选择许多Python开发者都希望开发出能够在Windows和Linux系统上都能运行的桌面应用程...

Python沙漏图形绘制:如何避免变量未定义错误? Python沙漏图形绘制:如何避免变量未定义错误? Apr 01, 2025 pm 06:27 PM

Python入门:沙漏图形绘制及输入校验本文将解决一个Python新手在沙漏图形绘制程序中遇到的变量定义问题。代码...

Google和AWS是否提供公共PyPI镜像源? Google和AWS是否提供公共PyPI镜像源? Apr 01, 2025 pm 05:15 PM

云服务商提供的PyPI镜像源许多开发者依赖PyPI(PythonPackageIndex)...

See all articles