简体   繁体   中英

AttributeError: object has no attribute Python Error

I have this code but it doesn't work fine.

POINT

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

BOMB:

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

When i start Bomb Thread, this program gives me this error: AttributeError: 'Bomb' object has no attribute '_coordinate' I presume the error is hash .

Full stack trace:

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'

How i can resolve?

The problem is caused by the order of parent initializers in Bomb . Reversing the order eliminates the error:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM