AttributeError: object has no attribute Python Error

AttributeError: object has no attribute Python Error

我有这段代码,但它不能正常工作。

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())

PointType

class PointType(Point):

    def __init__(self, i: int, j: int, t: int):
        Point.__init__(self, i, j)
        self.__t = t

    # getters and setters

炸弹:

class Bomb(Thread, PointType):

    def __init__(self, i: int, j: int):
        Thread.__init__(self)
        PointType.__init__(self, i, j, BOMB)

    def run(self) -> None:
        # code

当我启动 Bomb Thread 时,这个程序给我这个错误:AttributeError: 'Bomb' object has no attribute '_coordinate' 我认为错误是 hash.

完整堆栈跟踪:

Traceback (most recent call last): File "D:\Mario\Documents\BomberFriends\application\model\games.py", line 634, in plant() File "D:\Mario\Documents\BomberFriends\application\model\games.py", line 589, in plant gameInstance.plantBomb(i, j) File "D:\Mario\Documents\BomberFriends\application\model\games.py", line 151, in plantBomb Bomb(i, j).start() File "D:\Mario\Documents\BomberFriends\application\model\games.py", line 238, in init Thread.init(self) File "C:\Users\Mario\AppData\Local\Programs\Python\Python39\lib\threading.py", line 822, in init _dangling.add(self) File "C:\Users\Mario\AppData\Local\Programs\Python\Python39\lib_weakrefset.py", line 85, in add self.data.add(ref(item, self._remove)) File "D:\Mario\Documents\BomberFriends\application\model\games.py", line 97, in hash return hash(self.__key()) File "D:\Mario\Documents\BomberFriends\application\model\games.py", line 89, in __key return self.get_i(), self.get_j() File "D:\Mario\Documents\BomberFriends\application\model\games.py", line 69, in get_i return self._coordinate[Point.I] AttributeError: 'Bomb' object has no attribute '_coordinate'

我该如何解决?

问题是由 Bomb 中父初始化器的顺序引起的。颠倒顺序消除错误:

class Bomb(Thread, PointType):
    def __init__(self, i: int, j: int):
        PointType.__init__(self, i, j, Bomb)
        Thread.__init__(self)