TypeError:'list'对象在python中是不可调用的

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

 example = list('easyhoss') 

现在,在教程中, example= ['e','a',...,'s'] 。 但在我的情况下,我得到以下错误:

 >>> example = list('easyhoss') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'list' object is not callable 

请告诉我我错在哪里。 我搜查了这个,但它是不同的。

好像你已经在代码中的某个地方通过实例名称来映射内置的名字list

 >>> example = list('easyhoss') >>> list = list('abc') >>> example = list('easyhoss') Traceback (most recent call last): File "<string>", line 1, in <module> TypeError: 'list' object is not callable 

我相信这是相当明显的。 Python在字典中存储对象名称(函数和类也是对象)(命名空间是作为字典实现的),因此,您可以在任何范围内重写任意名称。 它不会显示为某种错误。 你最好使用IDE(例如PyCharm的免费版本或Atom与Python插件)来突出显示名称遮蔽。

编辑 。 感谢您的其他问题,我认为关于built-ins和范围确定的整个事情是值得澄清的。 正如你可能知道的,Python强调“特例不足以打破规则”。 你面临的问题背后有一些规则。

  1. 命名空间 。 Python支持嵌套的命名空间。 理论上你可以无限地嵌套命名空间。 正如我已经提到的,名字空间基本上是名字的字典和对相应对象的引用。 你创build的任何模块都有自己的“全局”命名空间。 事实上,这只是一个与特定模块相关的本地命名空间。

  2. 范围界定 。 当你调用一个名字时,Python会在本地名字空间(相对于调用)中查找,如果它找不到名字,它会在较高级别的名字空间中重复尝试。 built-in函数和类驻留在特殊的高阶命名空间__builtins__ 。 如果您在模块的全局名称空间中声明了list ,则解释器将永远不会在__builtins__的较高级别名称空间中search该名称。 同样,假设你在模块中的一个函数内部创build了一个variablesvar ,在模块中另外创build了一个variablesvar 。 那么如果你在函数内部调用var ,Python永远不会给你全局variables,因为在本地名字空间中有一个variables – 它不需要在更高级别的名字空间中search。

这是一个简单的例子。

  >>> example = list("abc") # Works fine # Creating name "list" in the global namespace of the module >>> list = list("abc") >>> example = list("abc") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'list' object is not callable # Python looks for "list" and finds it in the global namespace. # But it's not the proper "list". # Let's remove "list" from the global namespace >>> del(list) # Since there is no "list" in the global namespace of the module, # Python goes to a higher-level namespace to find the name. >>> example = list("abc") # It works. 

所以,如你所见, built-ins没有什么特别之处。 而你的情况只是一个普遍规则的例子。

PS

当你开始交互式Python会话时,你创build一个临时模块。

对我来说,这是一个瓶服务器返回一些videos数组(我希望是在JSON格式..)

添加json.dumps(videos)解决了这个问题