经常有朋友问,学 Python 面向对象时,翻阅别人代码,会发现一个 super() 函数,那这个函数的作用到底是什么?
super() 函数的用途如下,在子类中调用父类的方法,多用于类的继承关系。
其语法格式如下所示:
super(type[, object-or-type])
参数说明如下:
返回值是代理对象。
可以直接查询官方帮助手册:
help(super)
输出信息如下所示:

Help on class super in module builtins: class super(object) | super() -> same as super(__class__,) | 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().meth(arg) | This works for class methods too: | class C(B): | @classmethod | def cmeth(cls, arg): | super().cmeth(arg)

对输出结果进行分析之后,可以得到如下结论:
直接看一下单继承相关代码,其中使用类名去调用父类方法。

class A:
def funA(self):
print("执行 A ,输出橡皮擦")
class B(A):
def funB(self):
# self 表示 B 类的实例
A.funA(self)
print("执行 B ,输出铅笔")
b = B()
b.funB()

上述代码在 B 类中增加了 funB 函数,并且去调用 A 类中的 funA 函数,此时输出的内容如下所示:
执行 A ,输出橡皮擦 执行 B ,输出铅笔