[英]What does "TypeError 'xxx' object is not callable" means?
As a starting developer in Python I've seen this error message many times appearing in my console but I don't fully understand what does it means.作为 Python 的初级开发人员,我在控制台中多次看到此错误消息,但我不完全理解它的含义。
Could anyone tell me, in a general way, what kind of action produces this error?谁能告诉我,一般来说,什么样的动作会产生这个错误?
That error occurs when you try to call, with ()
, an object that is not callable .当您尝试使用()
调用不可调用的对象时会发生该错误。
A callable object can be a function or a class (that implements __call__
method).可调用对象可以是函数或类(实现__call__
方法)。 According to Python Docs :根据Python 文档:
object.__call__(self[, args...]) : Called when the instance is “called” as a function object.__call__(self[, args...]) :当实例作为函数被“调用”时调用
For example:例如:
x = 1
print x()
x
is not a callable object, but you are trying to call it as if it were it. x
不是一个可调用的对象,但您试图将其称为可调用对象。 This example produces the error:此示例产生错误:
TypeError: 'int' object is not callable
For better understaing of what is a callable object read this answer in another SO post.为了更好地了解什么是可调用对象,请在另一篇 SO 帖子中阅读此答案。
The other answers detail the reason for the error.其他答案详细说明了错误的原因。 A possible cause (to check) may be your class has a variable and method with the same name, which you then call.一个可能的原因(检查)可能是你的类有一个同名的变量和方法,然后你调用它们。 Python accesses the variable as a callable - with ()
. Python 将变量作为可调用对象访问 - 使用()
。
eg Class A defines self.a
and self.a()
:例如,A 类定义了self.a
和self.a()
:
>>> class A:
... def __init__(self, val):
... self.a = val
... def a(self):
... return self.a
...
>>> my_a = A(12)
>>> val = my_a.a()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>>
The action occurs when you attempt to call an object which is not a function, as with ()
.当您尝试调用不是函数的对象时会发生该操作,例如()
。 For instance, this will produce the error:例如,这将产生错误:
>>> a = 5
>>> a()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
Class instances can also be called if they define a method __call__
如果类实例定义了__call__
方法,也可以调用它们
One common mistake that causes this error is trying to look up a list or dictionary element, but using parentheses instead of square brackets, ie (0)
instead of [0]
导致此错误的一个常见错误是尝试查找列表或字典元素,但使用括号而不是方括号,即(0)
而不是[0]
The exception is raised when you try to call not callable object.当您尝试调用不可调用对象时会引发异常。 Callable objects are (functions, methods, objects with __call__
)可调用对象是(函数、方法、带有__call__
对象)
>>> f = 1
>>> callable(f)
False
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
I came across this error message through a silly mistake.我通过一个愚蠢的错误遇到了这个错误消息。 A classic example of Python giving you plenty of room to make a fool of yourself. Python 的一个经典例子,给你足够的空间让你自己出丑。 Observe:观察:
class DOH(object):
def __init__(self, property=None):
self.property=property
def property():
return property
x = DOH(1)
print(x.property())
Results结果
$ python3 t.py
Traceback (most recent call last):
File "t.py", line 9, in <module>
print(x.property())
TypeError: 'int' object is not callable
The problem here of course is that the function is overwritten with a property.这里的问题当然是函数被属性覆盖了。
这只是意味着某些东西不是可调用的对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.