Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'list' object is not callable in python

Tags:

python

list

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.

like image 849
khan shah Avatar asked Jun 27 '15 09:06

khan shah


People also ask

How do I make a list callable?

The Python “typeerror: 'list' object is not callable” error is raised when you try to access a list as if it were a function. To solve this error, make sure square brackets are used to access or change values in a list rather than curly brackets.

What does it mean when a list is not callable?

The most common scenario where Python throws TypeError: 'list' object is not callable is when you have assigned a variable name as “list” or if you are trying to index the elements of the list using parenthesis instead of square brackets.

Why is my object not callable Python?

The Python "TypeError: 'int' object is not callable" occurs when we try to call an integer value as a function. To solve the error, correct the assignment, make sure not to override the built-in int() function, and resolve any clashes between function and variable names.

How do I fix TypeError int object is not callable in Python?

But in Python, this would lead to the Typeerror: int object is not callable error. To fix this error, you need to let Python know you want to multiply the number outside the parentheses with the sum of the numbers inside the parentheses. Python allows you to specify any arithmetic sign before the opening parenthesis.


1 Answers

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.

like image 72
Eli Korvigo Avatar answered Sep 23 '22 21:09

Eli Korvigo