栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python3 super().__init__()_python super鍑芥暟鐢ㄦ硶?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python3 super().__init__()_python super鍑芥暟鐢ㄦ硶?

Python 3 内置函数 - super()函数 0. super()`函数

用于调用父类的一个方法。super() 是用来解决多重继承问题的。

1. 使用方法
>>> super?

# output:
Init signature: super(self, /, *args, **kwargs)
## 使用说明
Docstring:     
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)
Type:           type
Subclasses:   
2. 使用示例
# 定义一个base类. 有一个计算平方的方法。
>>> class base:
>>>     def square(self, x):
>>>         print("square (base):", x*x)

# 定义一个类A. 有两个方法。
>>> class classA(base):
>>>     def square(self, x):
>>>         super().square(x)
>>>     def add_one(self, x):
>>>         print(x+1)

# 生成实例
>>> a = classA()
>>> a.square(3)

# output:
square (base): 9
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/783178.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号