python - 访问超类属性 [英] python - accessing superclass attributes
本文介绍了python - 访问超类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我想在python中实现以下功能,但我无法弄清楚用 super.a = b
替换行:
Hi I want to achieve the following in python, however I cant figure out what to replace the line super.a = b
with:
class Super:
def __init__(self):
self.a = 1
class Sub(Super):
def method(self, b):
super.a = b
推荐答案
Sub
是 超级
,即 Sub
的所有实例都可以被视为与 Super
的实例完全相同。在您的情况下,这意味着您只需设置 self.a = b
。
An Sub
is a Super
, i.e. all instances of Sub
can be treated exactly like instances of Super
. In your case, that means you simply set self.a = b
.
这篇关于python - 访问超类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文