Python 我收到“类型错误:‘列表’对象不可调用”。我该如何解决这个错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45740182/
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
I'm getting "TypeError: 'list' object is not callable". How do I fix this error?
提问by Christian Dean
I have a simple script:
我有一个简单的脚本:
list = [1, 2, 3, 4, 5]
myrange = list(range(1, 10))
for number in list:
if number in myrange:
print(number, 'is between 1 and 10')
However, whenever I attempt to run my script, Python raises an error:
但是,每当我尝试运行脚本时,Python 都会引发错误:
Traceback (most recent call last):
File "python", line 2, in <module>
TypeError: 'list' object is not callable
What does this error mean? Why am I getting it? And how can I fix it?
这个错误是什么意思?为什么我得到它?我该如何解决?
回答by Christian Dean
Before you can fully understand what the error means and how to solve, it is important to understand what a built-in name is in Python.
在您完全理解错误的含义以及如何解决之前,了解 Python 中的内置名称是什么很重要。
What is a built-in name?
什么是内置名称?
In Python, a built-in name is a name that the Python interpreter already has assigned a predefined value. The value can be either a functionor classobject. These names are always made available by default, no matter the scope. Some of the values assigned to these names represent fundamental types of the Python language, while others are simple useful.
在 Python 中,内置名称是Python 解释器已经分配了预定义值的名称。该值可以是函数或类对象。这些名称在默认情况下始终可用,无论范围如何。分配给这些名称的一些值代表 Python 语言的基本类型,而另一些则简单有用。
As of the latest version of Python - 3.6.2- there are currently 61built-in names. A full list of the names and how they should be used, can be found in the documentation section Built-in Functions.
截至最新版本的 Python - 3.6.2- 目前有61 个内置名称。完整的名称列表以及如何使用它们,可以在文档部分内置函数中找到。
An important point to note however, is that Python will notstop you from re-assigning builtin names. Built-in names are not reserved, and Python allows them to be used as variable names as well.
然而,需要注意的重要一点是,Python不会阻止您重新分配内置名称。内置名称不是保留的,Python 也允许将它们用作变量名称。
Here is an example using the dict
built-in:
这是使用dict
内置的示例:
>>> dict = {}
>>> dict
{}
>>>
As you can see, Python allowed us to assign the dict
name, to reference a dictionary object.
如您所见,Python 允许我们分配dict
名称,以引用字典对象。
What does "TypeError: 'list' object is not callable" mean?
“TypeError:'list' 对象不可调用”是什么意思?
To put it simply, the reason the error is occurring is because you re-assignedthe builtin namelist
in the script:
简单地说,发生错误的原因是因为您在脚本中重新分配了内置名称list
:
list = [1, 2, 3, 4, 5]
When you did this, you overwrote the predefined value of the built-in name. This means you can no longer use the predefined value of list
, which is a class object representing Python list.
当您这样做时,您覆盖了内置 name 的预定义值。这意味着您不能再使用 的预定义值list
,它是表示 Python 列表的类对象。
Thus, when you tried to use the list
class to create a new list from a range
object:
因此,当您尝试使用list
该类从range
对象创建新列表时:
myrange = list(range(1, 10))
Python raised an error. The reason the error says "'list' object is not callable", is because as said above, the name list
was referring to a list object. So the above would be the equivalent of doing:
Python 引发了错误。错误显示“'list' 对象不可调用”的原因是因为如上所述,该名称list
指的是一个列表对象。所以上面的内容相当于做:
[1, 2, 3, 4, 5](range(1, 10))
Which of course makes no sense. You cannot call a list object.
这当然没有意义。您不能调用列表对象。
How can I fix the error?
我该如何修复错误?
If you are getting a similar error such as this one saying an "object is not callable", chances are you used a builtin name as a variable in your code. In this case the fix is as simple as renaming the offending variable. For example, to fix the above code, we could rename our list
variable to ints
:
如果您遇到类似的错误,例如“对象不可调用”,那么您很可能在代码中使用了内置名称作为变量。在这种情况下,修复就像重命名有问题的变量一样简单。例如,要修复上述代码,我们可以将list
变量重命名为ints
:
ints = [1, 2, 3, 4, 5] # Rename "list" to "ints"
myrange = list(range(1, 10))
for number in ints: # Renamed "list" to "ints"
if number in myrange:
print(number, 'is between 1 and 10')
PEP8- the official Python style guide - includes many recommendations on naming variables.
PEP8- 官方 Python 风格指南 - 包括许多关于命名变量的建议。
This is a very common error new and old Python users make. This is why it's important to always avoid using built-in names as variables such as str
, dict
, list
, range
, etc.
这是新老 Python 用户常犯的错误。这就是为什么总是避免使用很重要内置的名称作为变量,如str
,dict
,list
,range
,等。
Many linters and IDEs will warn you when you attempt to use a built-in name as a variable. If your frequently make this mistake, it may be worth your time to invest in one of these programs.
当您尝试使用内置名称作为变量时,许多 linter 和 IDE 会警告您。如果您经常犯这个错误,那么花时间投资其中一个程序可能是值得的。
I didn't rename a built-in name, but I'm still getting "TypeError: 'list' object is not callable". What gives?
我没有重命名内置名称,但我仍然收到“TypeError: 'list' object is not callable”。是什么赋予了?
Another common cause for the above error is attempting to index a list using parenthesis (()
) rather than square brackets ([]
). For example:
上述错误的另一个常见原因是尝试使用括号 ( ()
) 而不是方括号 ( []
)来索引列表。例如:
>>> lst = [1, 2]
>>> lst(0)
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
lst(0)
TypeError: 'list' object is not callable
For an explanation of the full problem and what can be done to fix it, see TypeError: 'list' object is not callable while trying to access a list.
有关完整问题的说明以及如何修复它,请参阅TypeError: 'list' object is not callable while try to access a list。
回答by Gribouillis
Here is the mcve!
这是mcve!
>>> []()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
Try also {}()
and ()()
. The message TypeError: 'X' object is not callable
means that you wrote expression(some_arguments)
where expression
is an instance of the X
type, and this type doesn't supportto be used in a function call syntax. Most of the time you wrote this because you thought expression
was a function or some other callable type.
也试试{}()
和()()
。该消息TypeError: 'X' object is not callable
表示您编写了expression(some_arguments)
whereexpression
是该X
类型的实例,并且该类型不支持在函数调用语法中使用。大多数时候你写这个是因为你认为它expression
是一个函数或其他一些可调用类型。