如何解决属性错误:对象没有属性 Python 错误
我有这个代码,但它不能正常工作。
要点
class Point:
I = 0
J = 1
def __init__(self,i: int,j: int):
self._coordinate = [i,j] # list
# ..other code (getter and setter)
def __key(self):
return self.get_i(),self.get_j()
def __eq__(self,other):
if isinstance(other,Point):
return self.__key() == other.__key()
return NotImplemented
def __hash__(self):
return hash(self.__key())
点类型
class PointType(Point):
def __init__(self,j: int,t: int):
Point.__init__(self,i,j)
self.__t = t
# getters and setters
炸弹:
class Bomb(Thread,PointType):
def __init__(self,j: int):
Thread.__init__(self)
PointType.__init__(self,j,BOMB)
def run(self) -> None:
# code
当我启动 Bomb Thread 时,这个程序给了我这个错误:AttributeError: 'Bomb' object has no attribute '_coordinate' 我认为错误是 hash。
完整的堆栈跟踪:
回溯(最近一次调用最后一次):文件 “D:\Mario\Documents\BomberFriends\application\model\games.py”,行 634,在 植物()文件“D:\Mario\Documents\BomberFriends\application\model\games.py”,行 589,厂内 gameInstance.plantBomb(i,j) 文件“D:\Mario\Documents\BomberFriends\application\model\games.py”,行 151、植物炸弹 Bomb(i,j).start() 文件“D:\Mario\Documents\BomberFriends\application\model\games.py”,行 238,在初始化 Thread.init(self) 文件“C:\Users\Mario\AppData\Local\Programs\Python\Python39\lib\threading.py”, 第 822 行,在 init 中 _dangling.add(self) 文件“C:\Users\Mario\AppData\Local\Programs\Python\Python39\lib_weakrefset.py”, 第 85 行,添加 self.data.add(ref(item,self._remove)) 文件“D:\Mario\Documents\BomberFriends\application\model\games.py”,行 97、在哈希 return hash(self.__key()) File "D:\Mario\Documents\BomberFriends\application\model\games.py",line 89,在__key 返回 self.get_i(),self.get_j() 文件“D:\Mario\Documents\BomberFriends\application\model\games.py”,行 69、在get_i 返回 self._coordinate[Point.I] AttributeError: 'Bomb' 对象没有属性 '_coordinate'
我该如何解决?
解决方法
问题是由 Bomb
中父初始化器的顺序引起的。颠倒顺序消除错误:
class Bomb(Thread,PointType):
def __init__(self,i: int,j: int):
PointType.__init__(self,i,j,Bomb)
Thread.__init__(self)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。