Python “元组”不可调用错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23882477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'tuple' not callable error
提问by Loek Janssen
I now that this question has been asked several times. However, the answers do not seem to resolve my problem. I get a type error, 'tuple' object is not callable. I get this even though the tuple inside the list is separated by commas in the correct way:
我现在这个问题已经被问过好几次了。但是,答案似乎并没有解决我的问题。我收到类型错误,“元组”对象不可调用。即使列表中的元组以正确的方式用逗号分隔,我也得到了这个:
def aiMove(b):
movesList = moves(b, -1)
heuristic = []
if movesList:
for m in movesList:
bt = copy.deepcopy(b)
print("bt: ", bt)
bt[m[0]][m[1]] = -1
h = heat[m[0]][m[1]]
for move in m[2]:
i=1;
try:
while (-1* bt[m[0] + i*move[0]][m[1] + i*move[1]] < 0):
bt[m[0] + i*move[0]][m[1] + i*move[1]] *= -1
bt[m[0] + i*move[0]][m[1] + i*move[1]] += -1
i += 1;
except IndexError:
continue
alpha = max(float('-inf'), alphabeta(bt, depth-1, h, float('-inf'), float('inf'), 1))
heuristic.append(alpha)
if (float('inf') <= alpha):
break
selectedMove = movesList[int(heuristic.index(max(heuristic)))]
move(b, selectedMove, -1)
else:
print ("The AI can't move!")
return []
selectedMove is (3, 2, [(0 , 1)]) for example. The moves() function returns a list of moves like the final value for selectedMove. It is an alpha beta pruning implementation for game tree search. The move() function actually moves the piece to that position and updates the board to finish the computers' turn. the variable b represents values on the current game board Every conversion I try (i.e. force selectedMove to be a list in itself, etc..) will give the same error in the line "move(b, selectedMove, -1)"
selectedMove 是 (3, 2, [(0, 1)]) 例如。move() 函数返回一个移动列表,例如 selectedMove 的最终值。它是用于博弈树搜索的 alpha beta 修剪实现。move() 函数实际上将棋子移动到那个位置并更新棋盘以完成计算机的回合。变量 b 代表当前游戏板上的值我尝试的每次转换(即强制 selectedMove 本身成为一个列表,等等)都会在“move(b, selectedMove, -1)”行中给出相同的错误
Does somebody see what may be wrong?
有人看到可能有什么问题吗?
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1489, in __call__
return self.func(*args)
File "C:\Users\Loek Janssen\Documents\GitHub\CITS1401-Project\DrawBoard.py", line 131, in eventfun
placePiece(x, y, b, n)
File "C:\Users\Loek Janssen\Documents\GitHub\CITS1401-Project\DrawBoard.py", line 119, in placePiece
project2.aiMove(b)
File "C:\Users\Loek Janssen\Documents\GitHub\CITS1401-Project\project2.py", line 225, in aiMove
move(b, selectedMove, -1)
TypeError: 'tuple' object is not callable
回答by DSM
In this line, you say that you're going to use the name moveto refer to elements of m[2]:
在这一行中,您说您将使用名称move来引用 的元素m[2]:
for move in m[2]:
But then later, you try to call a functionthat you've called move:
但后来,你试图调用一个函数,你已经叫move:
move(b, selectedMove, -1)
Once you've seen this, the error message makes complete sense:
一旦你看到了这一点,错误消息就完全有意义了:
TypeError: 'tuple' object is not callable
because the name movedoesn't refer to the function any more, but to the last tuple it was bound to in the loop.
因为该名称move不再引用该函数,而是引用它在循环中绑定到的最后一个元组。
More important than fixing this particular error (don't use movefor a variable name if you also want to use it to refer to a function) is recognizing how the interpreter told you exactly what the problem was.
比修复此特定错误(move如果您还想用它来引用函数,请不要用于变量名)更重要的是识别解释器如何准确地告诉您问题是什么。
It said that a tuple wasn't callable; in the line of code it complained about, you were calling move; thus movewas a tuple, and your next step should've been adding print(move)(or print(type(move), repr(move))if you want to be fancy) to see what tuple it actually was.
它说元组不可调用;在它抱怨的代码行中,您正在调用move; 因此move是一个元组,您的下一步应该是添加print(move)(或者,print(type(move), repr(move))如果您想变得花哨)以查看它实际上是什么元组。

