运行方法时将属性设置为“无”

2024-06-14 23:27:40 发布

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

我正在livewires中编写一个chess程序,当我尝试运行一个函数时,一个属性(应该包含一个对象)突然被设置为None。下面是代码中导致问题的方法的一个片段:

from livewires import games
....
def toggle(self):
    piece = self.hold
    ...
    elif piece.type == "K":
            if "CASTLE_R" in piece.moveset:
                 #piece is still an object, i used print
                 piece.hasmoved = True 
                 #piece still an object
                 self.boxremove() 
                 ...

def boxremove(self):
    piece = self.hold
    #suddenly piece is not an object
    print self.hold
    for item in piece.poss: #here it says an error that piece is NoneType
        item.destroy()
        ...

我很困惑为什么它突然变成了一个非类型,因为我没有做任何事情来编辑这些部分之间的它。你知道吗

完整的代码可以在这里找到:https://www.dropbox.com/s/rzhq9fek7qtzims/chess.py?dl=0 在210线附近。你知道吗


Tags: 代码inselfanpieceobjectisdef
1条回答
网友
1楼 · 发布于 2024-06-14 23:27:40

好吧,你在你的类__init__中设置了self.hold = None。这意味着有一个地方/情况,它从来没有被设置,并保持None,直到您的错误点。为了找到问题,您必须在比所显示的代码更大的代码段中进行调试。你知道吗

相关问题 更多 >