【问题标题】:TypeError: 'list' object is not callable when trying to call an object from a dictionaryTypeError:尝试从字典中调用对象时,“列表”对象不可调用
【发布时间】:2014-10-27 13:14:09
【问题描述】:

我正在尝试为学校作业编写国际象棋的实现,但我遇到了一个我似乎无法解决的问题。

def askpiece(self,player):
    inputstring=input(player.name+ ", what is the location of the piece you would like to move (e.g.)? ")
    x,y=inputstring
    y=int(y)
    if (x,y) in self and self[(x,y)].name[1] == player.tag:
        if self[(x,y)].canmove(self,player):
            return (x,y)
        else:
            print("the selected piece currently can't move, try again.")
    elif self[(x,y)].name[1] != player.tag:
        print("the piece you are trying to move belongs to the other player, try again.")
        self.askpiece(player)
    elif (x,y) not in self:
        print("there is currently no piece on the specified location, try again.")

def canmove(self,board,player):  #controls if the piece can move in atleast one way 

                                 #included the function canmove too in case that is what is causing the error
    lettertonumber={"a":1,"b":2,"c":3,"d":4}
    numbertoletter={1:"a",2:"b",3:"c",4:"d"}
    for move in self.canmove:
        if lettertonumber[self.x]+move[0] in [1,4] and self.y+move[1] in [1,5]:
            if (lettertonumber[self.x]+move[0],self.y+move[1]) in board:
                if self.name[1] != player.tag:
                    return True
            else:
                return True
    return False

当我调用此函数时,该函数会正确询问我要移动的棋子的位置(例如 b1 上的车),检查该棋子是否存在以及该棋子是否确实属于我,但随后生成一个 TypeError:

File "Schaak.py", line 31, in <module>
   game()
File "Schaak.py", line 27, in game
   (x,y)=askpiece(bord,player)
File "Schaak.py", line 9, in askpiece
   if board[(x,y)].canmove(board,player) == True:

TypeError: 'list' object is not callable

在 askpiece 中,self 是一个名为“board”的字典,而 self[(x,y)] 是字典中的一个棋子,当我要求代码打印 self[(x,y)] 时,它正确地指出对象是“类:Rook”,如果我要求它打印对象本身,输出也是正确的。无论我如何更改语法,我似乎都会收到此错误(除非我将其更改为产生不同错误的内容),并且在其余代码中,当我调用 self[(x,y)] 时,我不会收到任何错误

【问题讨论】:

  • 我无法在您的代码中找到if board[(x,y)].canmove(board,player) == True: 行。
  • 您是否在代码中的任何地方重复使用了名称 canmove 并将其分配给某个列表?
  • 是的!就是这样!我不敢相信我忽略了这一点。

标签: python dictionary typeerror


【解决方案1】:

canmove()中的这一行

for move in self.canmove:

暗示您在某个时候将此属性设置为列表。方法和实例变量存在于同一个命名空间中(实际上,一个方法只不过是一个恰好可以调用的 [class] 变量),因此您不能重用相同的名称。一个名字或另一个必须改变;由于列表变量似乎更有可能只在内部使用,我建议将其更改为 _canmove,除非有更合适的名称可供使用。

【讨论】:

  • 非常感谢,我将方法的名称更改为 can_move 并修复了整个问题!
  • 更好!这使得方法名称更符合 PEP8。
【解决方案2】:

代码中的某处canmove 已分配给list,因此当您尝试canmove(board,player) 时会出现错误:

In [26]: canmove = []

In [27]: canmove = []

In [28]: canmove()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-28-2a6706ff3740> in <module>()
----> 1 canmove()

TypeError: 'list' object is not callable

【讨论】:

  • 非常感谢,函数名称中的简单下划线修复了整个问题!
猜你喜欢
  • 1970-01-01
  • 2011-08-09
  • 2015-03-11
  • 1970-01-01
  • 2019-01-05
  • 2012-06-14
  • 1970-01-01
  • 1970-01-01
  • 2019-02-06
相关资源
最近更新 更多