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

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

问题描述

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

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

example = list('easyhoss')

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

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.

推荐答案

似乎您已将指向类的内置名称list指向其实例,从而掩盖了它的名称.这是一个示例:

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

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

I believe this is fairly obvious. Python stores object names (functions and classes are objects, too) in dictionaries (namespaces 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. 命名空间. Python支持嵌套名称空间.从理论上讲,您可以无休止地嵌套名称空间.正如我已经提到的,名称空间基本上是名称和对相应对象的引用的字典.您创建的任何模块都有其自己的全局"名称空间.实际上,它只是该特定模块的本地名称空间.

  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.

作用域.引用名称时,Python运行时将在本地名称空间(相对于引用)中查找该名称,如果该名称不存在,它将在更高级别的名称空间中重复该尝试.此过程将继续进行,直到没有更高的名称空间为止.在这种情况下,您会得到一个NameError.内置函数和类驻留在特殊的高级命名空间__builtins__中.如果在模块的全局命名空间中声明了名为list的变量,则解释器将永远不会在更高级别的命名空间(即__builtins__)中搜索该名称.同样,假设您在模块中的函数内部创建变量var,并在模块中创建另一个变量var.然后,如果在函数内部引用var,则永远不会获得全局var,因为本地名称空间中有一个var-解释器无需在其他位置搜索它.

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.

这是一个简单的例子.

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

因此,如您所见,Python内置函数没有什么特别之处.您的案例仅仅是通用规则的一个例子.最好使用一个IDE(例如带有Python插件的PyCharm或Atom的免费版本)来突出显示名称阴影以避免这种错误.

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.

您可能还想知道什么是可调用的",在这种情况下,您可以阅读以下文章: https://stackoverflow.com/a/111255/3846213 .作为类的list是可调用的.调用类会触发实例构造和初始化.一个实例也可能是可调用的,但list实例不是.如果您对类和实例之间的区别更加困惑,则可能需要阅读文档(非常方便,同一页面介绍了名称空间和作用域).

You might as well be wondering what is a "callable", in which case you can read the following post: https://stackoverflow.com/a/111255/3846213. 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).

如果您想进一步了解内建函数,请阅读Christian Dean的回答.

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

PS

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

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

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

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