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

Python 运行方法时将属性设置为“无”,python,python-2.7,attributes,nonetype,Python,Python 2.7,Attributes,Nonetype,我在livewires中制作一个国际象棋程序,当我尝试运行一个函数时,一个属性(应该包含一个对象)突然被设置为“无”。下面是代码中导致问题的方法的一个片段: from livewires import games .... def toggle(self): piece = self.hold ... elif piece.type == "K": if "CASTLE_R" in piece.moveset: #

我在livewires中制作一个国际象棋程序,当我尝试运行一个函数时,一个属性(应该包含一个对象)突然被设置为“无”。下面是代码中导致问题的方法的一个片段:

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()
        ...
我不明白为什么它突然变成了非类型,因为我没有做任何事情在这些部分之间编辑它

完整代码可在此处找到:
在第210行附近。

好的,您在类中设置了
self.hold=None
。这意味着在某个位置/情况下,它从未被设置,并一直保持
None
,直到您的错误点。您必须在比您显示的更大的代码部分中进行调试才能找到问题。

您是否尝试在调试器中运行它?@Selcuk是的,但它在livewires模块中由于某种原因崩溃。您的代码中有一些地方将其设置为
None
。您是否进行过调试以查看是否调用了这些语句?如果调试器无法正常工作,添加一些print语句可能会非常有启发性。@BryanOakley我也尝试过,在调用boxremove()函数时,它似乎会将其变为“无”。