【问题标题】:Attribute set to None while running method运行方法时属性设置为无
【发布时间】: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


【解决方案1】:

好吧,你在你的班级__init__ 中设置了self.hold = None。这意味着有一个地方/案例永远不会设置,并且一直保持None 直到您的错误点。您必须调试比您显示的代码更大的部分才能找到问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多