删除运行两次,但不执行任何操作

2024-03-28 10:39:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我想移动一个对象,但是删除不能正常工作。。。你知道吗

它给出以下错误: 索引器错误:从空列表中弹出。你知道吗

我得到了删除的代码:

def remove(xpos, ypos):
    pos = [xpos,ypos]
    if pos in objectpos:
        objid = objectpos.index(pos)
        objects.pop(objid)
        objectpos.pop(objid)

这个类应该调用remove:

class object:

    def __init__(self, xpos, ypos, char):
        if xpos <= 0 or xpos > width:
            raise PlacementError("Error, object out of bounds.")
        if ypos <= 0 or ypos > height:
            raise PlacementError("Error, object out of bounds.")
        self.pos = [xpos,ypos]
        remove(xpos, ypos)
        objects.append(self)
        objectpos.append(self.pos)
        self.xpos = xpos
        self.ypos = ypos
        self.char = char

    def vertical(self, dis):
        self.to_y = self.ypos + int(dis)
        object(self.to_y, self.xpos, self.char)
        remove(self.xpos, self.ypos)

    def horizontal(self, dis):
        self.to_x = self.xpos + int(dis)
        object(self.ypos, self.to_x, self.char)
        remove(self.xpos, self.ypos)

    def __del__(self):
        remove(self.xpos, self.ypos)

注意__del__没有被调用! 我只叫水平和垂直。你知道吗

我试着打印对象列表等,在错误发生之前输出所有内容两次。你知道吗

这可能不是最好的方法,但程序的速度并不重要,我只想知道为什么它运行两次,并给出一个错误。你知道吗

谢谢!你知道吗


Tags: to对象posselfifobjectdef错误