'Tuple' object is not callable - Python

笑着哭i 提交于 2021-02-11 13:53:18

问题


I'm using pygame and I'm using a function that set the selected position of a text in PyGame :

def textPos(YPos , TextSize):
    TextPosition.center(60,YPos)
    print("Size : " + TextSize)

but when I run the program, I get an error:

TextPosition.center(60,YPos) : TypeError : 'Tuple' object is not callable 

There's a way to solve this problem?


回答1:


'Tuple' object is not callable error means you are treating a data structure as a function and trying to run a method on it. TextPosition.center is a tuple data structure not a function and you are calling it as a method. If you are trying to access an element in TextPosition.Center, use square brackets []

For example:

foo = [1, 2, 3]
bar = (4, 5, 6)

# trying to access the third element with the wrong syntax
foo(2) --> 'List' object is not callable
bar(2) --> 'Tuple' object is not callable

# what I really needed was
foo[2]
bar[2]


来源:https://stackoverflow.com/questions/61775032/tuple-object-is-not-callable-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!