我收到“TypeError: 'list' object is not callable".我该如何解决这个错误? [英] I'm getting "TypeError: 'list' object is not callable". How do I fix this error?

查看:208
本文介绍了我收到“TypeError: 'list' object is not callable".我该如何解决这个错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的脚本:

list = [1, 2, 3, 4, 5]myrange = list(range(1, 10))对于列表中的数字:如果数字在我的范围内:打印(数字,'在 1 和 10 之间')

但是,每当我尝试运行脚本时,Python 都会引发错误:

回溯(最近一次调用最后一次): 中的文件python",第 2 行类型错误:列表"对象不可调用

这个错误是什么意思?为什么我得到它?我该如何解决?

解决方案

在您完全理解错误的含义以及如何解决之前,了解 Python 中的内置名称是很重要的.

什么是内置名称?

在 Python 中,内置名称是 Python 解释器已经分配了预定义值的名称.该值可以是 functionclass 对象.这些名称在默认情况下始终可用,无论范围如何.分配给这些名称的一些值代表 Python 语言的基本类型,而另一些则简单有用.

截至最新版本的 Python - 3.6.2 - 目前有 61 内置名称.完整的名称列表以及如何使用它们,可以在文档部分找到 内置函数.

但是需要注意的重要一点是,Python 不会阻止您重新分配内置名称.内置名称不是保留的,Python 也允许将它们用作变量名称.

这是一个使用 dict 的示例 内置:

<预><代码>>>>字典 = {}>>>字典{}>>>

如您所见,Python 允许我们分配 dict 名称,以引用字典对象.

TypeError:'list' 对象不可调用"是什么意思?

简单地说,发生错误的原因是因为您重新分配了内置名称 list:

list = [1, 2, 3, 4, 5]

执行此操作时,覆盖了内置名称的预定义值.这意味着您不能再使用 list 的预定义值,它是一个表示 Python 列表的类对象.

因此,当您尝试使用 list 类从 range 对象创建新列表时:

myrange = list(range(1, 10))

Python 引发了错误.错误显示'list' 对象不可调用"的原因是因为如上所述,名称 list 指的是一个列表对象.所以上面的内容相当于做:

[1, 2, 3, 4, 5](range(1, 10))

这当然没有意义.您不能调用列表对象.

如何修复错误?

如果您遇到类似的错误,例如对象不可调用",那么您很可能在代码中使用了内置名称作为变量.在这种情况下,修复就像重命名有问题的变量一样简单.例如,要修复上面的代码,我们可以将 list 变量重命名为 ints:

ints = [1, 2, 3, 4, 5] # 将list"重命名为ints"myrange = list(range(1, 10))对于整数中的数字:# 将列表"重命名为整数"如果数字在我的范围内:打印(数字,'在 1 和 10 之间')

PEP8 - 官方 Python 风格指南 - 包括许多关于命名变量的建议.

这是新老 Python 用户常犯的错误.这就是为什么始终避免使用内置名称作为变量很重要的原因,例如 strdictlistrange

当您尝试使用内置名称作为变量时,许多 linter 和 IDE 会警告您.如果您经常犯此错误,那么您可能值得花时间投资其中一个程序.

我没有重命名内置名称,但我仍然收到TypeError: ‘list’ object is not callable".什么给?

上述错误的另一个常见原因是尝试使用括号 (()) 而不是方括号 ([]) 对列表进行索引.例如:

<预><代码>>>>lst = [1, 2]>>>列表(0)回溯(最近一次调用最后一次):文件<pyshell#32>",第 1 行,在 <module> 中列表(0)类型错误:列表"对象不可调用

有关完整问题的说明以及修复方法,请参阅 TypeError: 'list' 对象在尝试访问列表时不可调用.

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:

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?

解决方案

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.

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 function or class object. 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.

As of the latest version of Python - 3.6.2 - there are currently 61 built-in names. A full list of the names and how they should be used, can be found in the documentation section Built-in Functions.

An important point to note however, is that Python will not stop you from re-assigning builtin names. Built-in names are not reserved, and Python allows them to be used as variable names as well.

Here is an example using the dict built-in:

>>> dict = {}
>>> dict
{}
>>>

As you can see, Python allowed us to assign the dict name, to reference a dictionary object.

What does "TypeError: 'list' object is not callable" mean?

To put it simply, the reason the error is occurring is because you re-assigned the builtin name list in the script:

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.

Thus, when you tried to use the list class to create a new list from a range object:

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:

[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:

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.

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.

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.

I didn't rename a built-in name, but I'm still getting "TypeError: 'list' object is not callable". What gives?

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".我该如何解决这个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆