我正在运行 Windows 7,使用 python 2.7.3,我收到一个我无法弄清楚原因的继承错误。我已经做了很多搜索,但到目前为止还没有找到太多相关的东西。
我的问题是,当我尝试从一个类继承到另一个类时,我不断收到 AttributeError。我的基本结构是这样的:
# pymyClass.py
class myClass(object):
def __init__(self,aList=None):
if aList is not None:
myList = aList
else:
myList = ['','','','','']
def reset(self,aList=None):
if aList is not None:
myList = aList
else:
myList = ['','','','','']
#other methods operate without issue
# pymySecondClass.py
import pymyClass
class mySecondClass(pymyClass.myClass):
def __init__(self,aList=None):
pymyClass.myClass(aList)
# pymyThirdClass.py
import pymySecondClass
class myThirdClass(pymySecondClass.mySecondClass):
def __init__(self,aList=None):
pymySecondClass.mySecondClass(aList)
def useList(self,aList=None):
self.reset(aList)
print self.myList
#pymyObj.py
import pymyThirdClass
myObj = pymyThirdClass.myThirdClass(['a','b','c','1','2'])
myObj.useList()
...但是当我调用实例化 myThirdClass() 并调用 useList() 时,它会出错,说,
AttributeError: 'myThirdClass' object has no attribute 'myList'
我实际上在这里编译了我的示例,并遇到了同样的问题,所以我认为继承不能按我期望的方式工作。我已经检查了 python 文档,但可能还不够接近?如果有人可以在这里帮助我,将不胜感激。
我想我可能不得不在 myThirdClass 构造函数中手动包含字段“myList”,但这似乎非常糟糕。提前致谢!