类型错误:“列表"对象在 python 中不可调用 [英] TypeError: 'list' object is not callable in python

查看:43
本文介绍了类型错误:“列表"对象在 python 中不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,正在学习教程.教程中有一个list的例子:

example = list('easyhoss')

现在,在教程中,example= ['e','a',...,'s'].但就我而言,我收到以下错误:

<预><代码>>>>示例 = list('easyhoss')回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:列表"对象不可调用

请告诉我我错在哪里.我搜索了 SO this 但它是不同的.

解决方案

好像你隐藏了内置名称 list 指向一个类的同名指向它的实例.下面是一个例子:

<预><代码>>>>example = list('easyhoss') # 这里的`list`指的是内置类>>>list = list('abc') # 我们创建一个变量 `list` 引用一个 `list` 的实例>>>example = list('easyhoss') # 这里的`list`指的是实例回溯(最近一次调用最后一次):文件<string>",第 1 行,在 <module> 中.类型错误:列表"对象不可调用

我相信这是相当明显的.Python 在命名空间(实现为字典)中存储对象名称(函数和类也是对象),因此您几乎可以在任何范围内重写任何名称.它不会显示为某种错误.您可能知道,Python 强调特殊情况不足以打破规则".您所面临的问题背后有两个主要规则:

  1. 命名空间.Python 支持嵌套命名空间.理论上,您可以无休止地嵌套命名空间.正如我已经提到的,名称空间基本上是名称和对相应对象的引用的字典.您创建的任何模块都有自己的全局"模块.命名空间.实际上,它只是与该特定模块相关的本地命名空间.

  2. 范围.当您引用一个名称时,Python 运行时会在本地命名空间(相对于引用)中查找它,如果这样的名称不存在,它会在更高级别的命名空间中重复尝试.这个过程一直持续到没有更高的命名空间为止.在这种情况下,您会收到 NameError.内置函数和类驻留在特殊的高阶命名空间 __builtins__ 中.如果在模块的全局命名空间中声明一个名为 list 的变量,解释器将永远不会在更高级别的命名空间(即 __builtins__)中搜索该名称.类似地,假设您在模块的函数中创建了一个变量 var,并在模块中创建了另一个变量 var.那么,如果你在函数内部引用了 var,你将永远得不到全局的 var,因为在局部命名空间中有一个 var——解释器不需要在别处搜索它.

这是一个简单的说明.

<预><代码>>>>example = list("abc") # 工作正常>>>>>># 创建名称列表"在模块的全局命名空间中>>>list = list(abc")>>>>>>示例 = 列表(abc")回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中.类型错误:列表"对象不可调用>>># Python 寻找列表";并在全局命名空间中找到它,>>># 但它不是正确的列表".>>>>>># 让我们删除列表";来自全局命名空间>>>删除列表>>># 因为没有列表"在模块的全局命名空间中,>>># Python 去更高级别的命名空间查找名称.>>>示例 = list("abc") # 它有效.

所以,如您所见,Python 内置函数并没有什么特别之处.你的情况只是普遍规则的一个例子.您最好使用可突出显示名称阴影的 IDE(例如免费版本的 PyCharm,或带有 Python 插件的 Atom)以避免此类错误.

您可能想知道什么是可调用",在这种情况下,您可以阅读这篇文章.list 作为一个类,是可调用的.调用类会触发实例构建和初始化.一个实例也可能是可调用的,但 list 实例不是.如果您对类和实例之间的区别更加困惑,那么您可能需要阅读 文档(很方便,同一页涵盖了命名空间和范围).

如果您想了解有关内置函数的更多信息,请阅读 Christian Dean 的答案.

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

I am novice to Python and following a tutorial. There is an example of list in the tutorial :

example = list('easyhoss')

Now, In tutorial, example= ['e','a',...,'s']. But in my case I am getting following error:

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

Please tell me where I am wrong. I searched SO this but it is different.

解决方案

Seems like you've shadowed the builtin name list pointing at a class by the same name pointing at its instance. Here is an example:

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

I believe this is fairly obvious. Python stores object names (functions and classes are objects, too) in namespaces (which are implemented as dictionaries), hence you can rewrite pretty much any name in any scope. It won't show up as an error of some sort. As you might know, Python emphasizes that "special cases aren't special enough to break the rules". And there are two major rules behind the problem you've faced:

  1. Namespaces. Python supports nested namespaces. Theoretically you can endlessly nest namespaces. As I've already mentioned, namespaces are basically dictionaries of names and references to corresponding objects. Any module you create gets its own "global" namespace. In fact it's just a local namespace with respect to that particular module.

  2. Scoping. When you reference a name, the Python runtime looks it up in the local namespace (with respect to the reference) and, if such name does not exist, it repeats the attempt in a higher-level namespace. This process continues until there are no higher namespaces left. In that case you get a NameError. Builtin functions and classes reside in a special high-order namespace __builtins__. If you declare a variable named list in your module's global namespace, the interpreter will never search for that name in a higher-level namespace (that is __builtins__). Similarly, suppose you create a variable var inside a function in your module, and another variable var in the module. Then, if you reference var inside the function, you will never get the global var, because there is a var in the local namespace - the interpreter has no need to search it elsewhere.

Here is a simple illustration.

>>> 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.

So, as you see there is nothing special about Python builtins. And your case is a mere example of universal rules. You'd better use an IDE (e.g. a free version of PyCharm, or Atom with Python plugins) that highlights name shadowing to avoid such errors.

You might as well be wondering what is a "callable", in which case you can read this post. list, being a class, is callable. Calling a class triggers instance construction and initialisation. An instance might as well be callable, but list instances are not. If you are even more puzzled by the distinction between classes and instances, then you might want to read the documentation (quite conveniently, the same page covers namespaces and scoping).

If you want to know more about builtins, please read the answer by Christian Dean.

P.S. When you start an interactive Python session, you create a temporary module.

这篇关于类型错误:“列表"对象在 python 中不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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