栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Python 3中调用super()的4种方法中的哪一种?

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

使用Python 3中调用super()的4种方法中的哪一种?

让我们使用以下类进行演示:

class A(object):    def m(self):        print('m')class B(A): pass

未绑定

super
对象不会将属性访问分配给类,您必须使用描述符协议:

>>> super(B).mTraceback (most recent call last):  File "<stdin>", line 1, in <module>AttributeError: 'super' object has no attribute 'm'>>> super(B).__get__(B(), B)<super: <class 'B'>, <B object>>

super
绑定到实例的对象提供了绑定方法:

>>> super(B, B()).m<bound method B.m of <__main__.B object at 0xb765dacc>>>>> super(B, B()).m()m

super
绑定到类的对象给出函数(根据Python 2的未绑定方法):

>>> super(B, B).m<function m at 0xb761482c>>>> super(B, B).m()Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: m() takes exactly 1 positional argument (0 given)>>> super(B, B).m(B())m

见米歇尔Simionato的“事情了解Python的超级”博客文章系列(1,2,3)了解更多信息



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

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

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