TypeError: 'list' object is not callable i

2019-01-02 21:18发布

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.

标签: python list
4条回答
回忆,回不去的记忆
2楼-- · 2019-01-02 21:36

For me it was a flask server returning some videos array (which I expected to be in json format..)

adding json.dumps(videos) fixed this issue

查看更多
人气声优
3楼-- · 2019-01-02 21:37

You may have used built-in name 'list' for a variable in your code. If you are using Jupyter notebook, sometimes even if you change the name of that variable from 'list' to something different and rerun that cell, you may still get the error. In this case you need to restart the Kernal. In order to make sure that the name has change, click on the word 'list' when you are creating a list object and press Shit+Tab, and check if Docstring shows it as an empty list.

Used Built-in list name for my list

Change my list name to my_list but still get error, Shift+Tab shows that list still hold the values of my_list

Restart Kernal and it's all good

查看更多
旧人旧事旧时光
4楼-- · 2019-01-02 21:46

Seems like you've shadowed the built-in name list by an instance name somewhere in your code.

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

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. You'd better use an IDE (e.g. a free version of PyCharm or Atom with Python plugins) that highlights name shadowing.

EDIT. Thanks to your additional questions I think the whole thing about builtins and scoping is worth some additional coverage. As you might know Python emphasizes that "special cases aren't special enough to break the rules". And there are a couple of 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.

P.S.

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

查看更多
后来的你喜欢了谁
5楼-- · 2019-01-02 21:59

If you are in a interactive session and don't want to restart you can remove the shadowing with

del list
查看更多
登录 后发表回答