Python “TypeError 'xxx' 对象不可调用”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21324940/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:36:08  来源:igfitidea点击:

What does "TypeError 'xxx' object is not callable" means?

python

提问by prl900

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?

谁能告诉我,一般来说,什么样的动作会产生这个错误?

采纳答案by Christian

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). According to Python Docs:

可调用对象可以是函数或类(实现__call__方法)。根据Python 文档

object.__call__(self[, args...]): Called when the instance is “called” as a function

object.__call__(self[, args...]):当实例作为函数被“调用”时调用

For example:

例如:

x = 1
print x()

xis not a callable object, but you are trying to call it as if it were it. This example produces the error:

x不是一个可调用的对象,但您试图将其称为可调用对象。此示例产生错误:

TypeError: 'int' object is not callable


For better understaing of what is a callableobject read this answer in another SO post.

为了更好地了解什么是可调用对象,请在另一篇 SO 帖子中阅读此答案。

回答by falsetru

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

回答by mhlester

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, i.e. (0)instead of [0]

一个常见的错误导致此错误是试图查找列表或字典元素,但使用圆括号而不是方括号,即(0)代替[0]

回答by wmorrison365

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 将变量作为可调用对象访问 - 使用().

e.g. Class A defines self.aand self.a():

例如,A 类定义self.aself.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
>>>

回答by krazekidg

It just means something isn't a callable object

它只是意味着某些东西不是可调用的对象