互联网技术 / 互联网资讯 · 2024年1月19日

Python内置函数-super() 函数的云计算开发指南

supeR() 函数是用于调用父类(超类)的一个方法。

supeR() 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO)、重复调用(钻石继承)等种种问题。

MRO 就是类的方法解析顺序表, 其实也就是继承父类方法时的顺序表。

以下是 supeR() 方法的语法:

supeR(type[, object-oR-type])

ype — 类。

object-oR-type — 类,一般是 self

Python3.x 和 Python2.x 的一个区别是: Python 3 可以使用直接使用 supeR().xxx 代替 supeR(ClaSS, self).xxx :

云计算开发:Python内置函数-super() 函数详解

云计算开发:Python内置函数-super() 函数详解

以下展示了使用 supeR 函数的实例:

云计算开发:Python内置函数-super() 函数详解

执行结果:

云计算开发:Python内置函数-super() 函数详解

OpenMagic API

Need more than content? Move into the product flow.

If you are here for model access, pricing, developer docs, or the future API console, the dedicated product path now lives on api.openmagic.ai.