Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类型错误:';列表';对象在python中不可调用_Python_List - Fatal编程技术网

类型错误:';列表';对象在python中不可调用

类型错误:';列表';对象在python中不可调用,python,list,Python,List,我是Python新手,并遵循教程。本教程中有一个list示例: example = list('easyhoss') 现在,在教程中,示例=['e','a',…,'s']。但在我的例子中,我得到了以下错误: >>> example = list('easyhoss') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'list' ob

我是Python新手,并遵循教程。本教程中有一个
list
示例:

example = list('easyhoss')
现在,在教程中,
示例=['e','a',…,'s']
。但在我的例子中,我得到了以下错误:

>>> example = list('easyhoss')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
Traceback (most recent call last):
  File "python", line 2, in <module>
TypeError: 'list' object is not callable
>示例=列表('easyhoss')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:“列表”对象不可调用

请告诉我哪里错了。我是这样搜索的,但结果不同。

似乎您已经用指向其实例的相同名称对指向某个类的内置名称
列表进行了跟踪。以下是一个例子:

>>> example = list('easyhoss')  # here `list` refers to the builtin class
>>> list = list('abc')  # we create a variable `list` referencing an instance of `list`
>>> example = list('easyhoss')  # here `list` refers to the instance
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: 'list' object is not callable
因此,正如您所看到的,Python内置没有什么特别之处。你的案例只是一个普遍规则的例子。您最好使用一个IDE(例如,PyCharm的免费版本,或者带有Python插件的Atom)来突出显示名称阴影,以避免此类错误

你可能想知道什么是“可调用”,在这种情况下你可以阅读<代码>列表
,作为一个类,是可调用的。调用类会触发实例构造和初始化。实例也可以是可调用的,但
list
实例不是。如果您对类和实例之间的区别感到更加困惑,那么您可能需要阅读(非常方便,相同的页面包含名称空间和范围)

如果您想了解更多关于内置的信息,请阅读下面的答案


p.S.当您启动交互式Python会话时,您将创建一个临时模块。

对我来说,这是一个flask服务器返回一些
视频
数组(我希望是json格式的..)


添加
json.dumps(视频)
修复了此问题

如果您正在进行交互式会话,并且不想重新启动,则可以使用

del list

您可能在代码中为变量使用了内置名称“list”。 如果您使用的是Jupyter笔记本,有时即使您将该变量的名称从“list”更改为其他名称并重新运行该单元格,也可能会出现错误。在这种情况下,需要重新启动内核。 为了确保名称已更改,请在创建列表对象时单击单词“list”,然后按Shift+Tab,然后检查Docstring是否将其显示为空列表


在您完全理解错误的含义和解决方法之前,了解Python中的内置名称很重要

什么是内置名称? 在Python中,内置名称是Python解释器已经分配了预定义值的名称。该值可以是函数或类对象。默认情况下,无论范围如何,这些名称始终可用。分配给这些名称的一些值表示Python语言的基本类型,而另一些值则非常有用

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

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

下面是一个使用内置组件的示例:

>>> dict = {}
>>> dict
{}
>>>
如您所见,Python允许我们指定
dict
名称来引用字典对象

“TypeError:“list”对象不可调用”是什么意思? 简单地说,发生错误的原因是您在脚本中重新分配了内置名称
列表

list = [1, 2, 3, 4, 5]
执行此操作时,重写了内置名称的预定义值。这意味着您不能再使用预定义的
list
,它是一个表示Python列表的类对象

因此,当您试图使用
列表
类从
范围
对象创建新列表时:

myrange = list(range(1, 10))
Python引发了一个错误。错误显示“列表”对象不可调用的原因是,如上所述,名称
list
指的是列表对象。因此,上述操作相当于:

[1, 2, 3, 4, 5](range(1, 10))
这当然毫无意义。不能调用列表对象

我如何修复错误? 假设您有如下代码:

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')
运行上述代码会产生以下错误:

>>> example = list('easyhoss')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
Traceback (most recent call last):
  File "python", line 2, in <module>
TypeError: 'list' object is not callable
-官方Python风格指南-包括许多关于命名变量的建议

这是新旧Python用户经常犯的错误。这就是为什么一定要避免使用内置名称作为变量,例如
str
dict
列表
范围
,等等

当您尝试使用内置名称作为变量时,许多linter和ide都会发出警告。如果你经常犯这样的错误,花时间投资其中一个项目可能是值得的

我没有重命名内置名称,但仍然得到“TypeError:“list”对象不可调用”。有什么好处? 上述错误的另一个常见原因是试图使用括号(
()
)而不是方括号(
[]
)为列表编制索引。例如:

>>> 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
>lst=[1,2]
>>>lst(0)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
lst(0)
TypeError:“列表”对象不可调用
有关完整问题的解释以及解决方法,请参阅。

为什么出现
TypeError:“list”对象不可调用?
说明:

这是因为您以前将
list
定义为一个变量(我很确定),所以它将是一个列表,不再是函数,这就是为什么每个人都不应该将变量命名为函数,以下内容与您现在所做的相同:

>>> [1,2,3]()
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    [1,2,3]()
TypeError: 'list' object is not callable
>>>  
如何检测变量名是否为函数?好吧,简单看看它是否有不同的颜色,或者使用如下代码:

>>> 'list' in dir(__builtins__)
True
>>> 'blah' in dir(__builtins__)
False
>>> 
此后,,
>>> 'list' in dir(__builtins__)
True
>>> 'blah' in dir(__builtins__)
False
>>> 
>>> list = [1,2,3]
>>> __builtins__.list()
[]
>>> 
>>> list = [1,2,3]
>>> []
[]
>>> 
>>> list = [1,2,3]
>>> del list
>>> list()
[]
>>> 
>>> lst = [1,2,3]
>>> list()
[]
>>> 
class DumbMistake:
    def __init__(self, k, v):
        self.k = k
        self.v = v
        self.update = []

    def update(self):
        // do updates to k, v, etc

if __name__ == '__main__':
    DumbMistake().update('one,two,three', '1,2,3')
l=[1,2,3]

print(l[2])#GOOD
print(l(2))#BAD
>>> print(list)
>>> del list
<class 'list'>