python - 访问超类属性

时间:2011-12-04 18:38:47

标签: python inheritance

您好我想在python中实现以下功能,但是我无法弄清楚用super.a = b代替行的内容:

class Super:
  def __init__(self):
    self.a = 1

class Sub(Super):
  def method(self, b):
    super.a = b

1 个答案:

答案 0 :(得分:15)

Sub Super,即Sub的所有实例都可以像Super的实例一样对待。在您的情况下,这意味着您只需设置self.a = b