Python中super关键字用法实例分析

作者:imzoer 时间:2023-12-08 06:11:46 

本文实例讲述了Python中super关键字用法。分享给大家供大家参考。具体分析如下:

在Python类的方法(method)中,要调用父类的某个方法,在Python 2.2以前,通常的写法如代码段1:

代码段1:


class A:
def __init__(self):
 print "enter A"
 print "leave A"
class B(A):
def __init__(self):
 print "enter B"
 A.__init__(self)
 print "leave B"
>>> b = B()

enter B
enter A
leave A
leave B

即,使用非绑定的类方法(用类名来引用的方法),并在参数列表中,引入待绑定的对象(self),从而达到调用父类的目的。

这样做的缺点是,当一个子类的父类发生变化时(如类B的父类由A变为C时),必须遍历整个类定义,把所有的通过非绑定的方法的类名全部替换过来,例如代码段2,

代码段2:


class B(C):  # A --> C
def __init__(self):
 print "enter B"
 C.__init__(self) # A --> C
 print "leave B"

如果代码简单,这样的改动或许还可以接受。但如果代码量庞大,这样的修改可能是灾难性的。很容易导致修改错误的出现。

因此,自Python 2.2开始,Python添加了一个关键字super,来解决这个问题。下面是Python 2.3的官方文档说明:


super(type[, object-or-type])
Return the superclass of type. If the second argument is omitted the super object
returned is unbound. If the second argument is an object, isinstance(obj, type)
must be true. If the second argument is a type, issubclass(type2, type) must be
true. super() only works for new-style classes.
A typical use for calling a cooperative superclass method is:
 class C(B):
   def meth(self, arg):
     super(C, self).meth(arg)
New in version 2.2.

从说明来看,可以把类B改写如代码段3:

代码段3:


class A(object):  # A must be new-style class
def __init__(self):
 print "enter A"
 print "leave A"
class B(C):   # A --> C
def __init__(self):
 print "enter B"
 super(B, self).__init__()
 print "leave B"

尝试执行上面同样的代码,结果一致,但修改的代码只有一处,把代码的维护量降到最低,是一个不错的用法。因此在我们的开发过程中,super关键字被大量使用,而且一直表现良好。

1. super并不是一个函数,是一个类名,形如super(B, self)事实上调用了super类的初始化函数,产生了一个super对象;

2. super类的初始化函数并没有做什么特殊的操作,只是简单记录了类类型和具体实例;

3. super(B, self).func的调用并不是用于调用当前类的父类的func函数;

4. Python的多继承类是通过mro的方式来保证各个父类的函数被逐一调用,而且保证每个父类函数只调用一次(如果每个类都使用super);

5. 混用super类和非绑定的函数是一个危险行为,这可能导致应该调用的父类函数没有调用或者一个父类函数被调用多次。

从super关键字的help我们也能看出来。

Help on class super in module __builtin__:
class super(object)
 |  super(type) -> unbound super object
 |  super(type, obj) -> bound super object; requires isinstance(obj, type)
 |  super(type, type2) -> bound super object; requires issubclass(type2, type)
 |  Typical use to call a cooperative superclass method:
 |  class C(B):
 |      def meth(self, arg):
 |           super(C, self).meth(arg)
 | 
 |  Methods defined here:
.......

从上面也能看出来,super是一个类,而且是__builtin__模块中的。

从上面的描述来看,super主要是用于调用父类的方法。

那么,在2.2之前的方法也能调用。为啥非得用super呢?

这是因为super能够阻止对父类方法的多次调用。

super,改变了父类搜索顺序, 返回的是一个特殊的父类对象
看例子:

class A: pass class B(A): pass class C(A):pass class D(B, C): pass

这是4个类的基本关系。

假如不使用super,让D的对象调用他们共有的一个方法,会2次调用A中这个方法。

希望本文所述对大家的Python程序设计有所帮助。

标签:Python,super,关键字
0
投稿

猜你喜欢

  • PHP获取url的函数代码

    2023-10-15 12:45:00
  • Python 聊聊socket中的listen()参数(数字)到底代表什么

    2022-10-17 00:49:25
  • ASP中将Excel数据导入到Access

    2009-02-02 09:15:00
  • Python中字典(dict)合并的四种方法总结

    2022-08-28 00:40:56
  • MySQL中SQL模式的特点总结

    2024-01-13 16:08:05
  • 关于Python形参打包与解包小技巧分享

    2021-06-08 21:43:39
  • 在Django中动态地过滤查询集的实现

    2023-06-15 12:20:15
  • 一文详解Golang协程调度器scheduler

    2024-04-30 10:05:58
  • Mysql使用kill命令解决死锁问题(杀死某条正在执行的sql语句)

    2024-01-24 06:36:03
  • JS与jQ读取xml文件的方法

    2024-04-19 10:13:22
  • 详解selenium + chromedriver 被反爬的解决方法

    2022-03-20 18:40:46
  • 在Python中使用Neo4j数据库的教程

    2024-01-15 00:40:54
  • 让Nginx支持ThinkPHP的URL重写和PATHINFO的方法分享

    2024-05-03 15:50:32
  • Jar包一键重启的Shell脚本及新服务器部署的一些经验分享

    2023-06-24 19:01:08
  • Python是编译运行的验证方法

    2021-03-15 04:25:10
  • MySQL实现字段或字符串拼接的三种方式总结

    2024-01-22 04:23:47
  • python+selenium+chromedriver实现爬虫示例代码

    2021-03-05 02:53:23
  • Vue基础学习之项目整合及优化

    2024-05-21 10:28:49
  • 浅谈MySQL安装starting the server失败的解决办法

    2024-01-25 06:37:22
  • Django添加bootstrap框架时无法加载静态文件的解决方式

    2023-04-16 07:27:44
  • asp之家 网络编程 m.aspxhome.com