TypeError: 'list' object is not callable in python

前端 未结 11 1572
醉梦人生
醉梦人生 2020-11-22 13:11

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

example = list(\'easyhoss\')

Now

相关标签:
11条回答
  • 2020-11-22 13:42

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

    del list
    
    0 讨论(0)
  • 2020-11-22 13:46

    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

    0 讨论(0)
  • 2020-11-22 13:48

    Close the current interpreter using exit() command and reopen typing python to start your work. And do not name a list as list literally. Then you will be fine.

    0 讨论(0)
  • 2020-11-22 13:51

    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.

    0 讨论(0)
  • 2020-11-22 13:52

    to solve the error like this one: "list object is not callable in python" even you are changing the variable name then please restart the kernel in Python Jutyter Notebook if you are using it or simply restart the IDE.

    I hope this will work. Thank you!!!

    0 讨论(0)
  • 2020-11-22 13:53

    find out what you have assigned to 'list' by displaying it

    >>> print(list)
    

    if it has content, you have to clean it with

    >>> del list
    

    now display 'list' again and expect this

    <class 'list'>
    

    Once you see this, you can proceed with your copy.

    0 讨论(0)
提交回复
热议问题