【发布时间】:2016-09-12 09:40:11
【问题描述】:
我正在用 livewires 制作一个国际象棋程序,当我尝试运行一个函数时,一个属性(应该包含一个对象)突然被设置为 None。这是我的代码中导致问题的方法的 sn-p:
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()
...
我很困惑为什么它突然变成了 NoneType,因为我没有在这些部分之间进行任何编辑。
完整代码可以在这里找到:https://www.dropbox.com/s/rzhq9fek7qtzims/chess.py?dl=0 在 210 号线附近。
【问题讨论】:
-
你试过在调试器中运行它吗?
-
@Selcuk 是的,但它在 livewires 模块中由于某种原因崩溃了。
-
代码中有一些地方将其设置为
None。您是否进行了任何调试以查看是否调用了这些语句中的任何一个?如果你不能让调试器工作,添加一些打印语句会很有启发性。 -
@BryanOakley 我也试过了,它似乎在调用 boxremove() 函数的那一刻将其变为 None。
标签: python python-2.7 attributes nonetype