我想從基類訪問一個變量。這裏的父類:Python:'super'對象沒有屬性'attribute_name'
class Parent(object):
def __init__(self, value):
self.some_var = value
而這裏的子類:
class Child(Parent):
def __init__(self, value):
super(Child, self).__init__(value)
def doSomething(self):
parent_var = super(Child, self).some_var
現在,如果我嘗試運行這段代碼:
obj = Child(123)
obj.doSomething()
我得到以下異常:
Traceback (most recent call last):
File "test.py", line 13, in <module>
obj.doSomething()
File "test.py", line 10, in doSomething
parent_var = super(Child, self).some_var
AttributeError: 'super' object has no attribute 'some_var'
我在做什麼錯誤?在Python中從基類訪問變量的推薦方式是什麼?