Before you can fully understand what the error means and how to solve it, it is important to understand what the built-in name is in Python.
What is an embedded name?
In Python, the built-in name is the name to which the Python interpreter has already assigned a predefined value . The value can be either a function or a class object. These names are always available by default, regardless of scope. Some of the values โโassigned to these names represent the fundamental types of the Python language, while others are just useful.
Starting with the latest version of Python - 3.6.2 - there are currently 61 built-in names. A complete list of names and how to use them can be found in the built-in functions documentation section.
However, itโs important to note that Python will not stop you from reassigning inline names . Built-in names are not reserved, and Python allows them to be used as variable names.
Here is an example using the inline dict :
>>> dict = {} >>> dict {} >>>
As you can see, Python allowed us to name the dict to refer to the dictionary object.
What does "TypeError: list object" are not called "?
Simply put, the reason for the error is that you reassigned the built-in list names in the script:
list = [1, 2, 3, 4, 5]
When you did this, you overwrite the predefined value of the built-in name . This means that you can no longer use the predefined value list , which is a class object representing a Python list.
Thus, when you tried to use the list class to create a new list from the range object:
myrange = list(range(1, 10))
Python raised an error. The reason the error says โthe list object is not calledโ is because, as mentioned above, the list names referred to the list object. Thus, the above would be equivalent to doing:
[1, 2, 3, 4, 5](range(1, 10))
Which, of course, does not make sense. You cannot call a list object.
How can I fix the error?
If you get a similar error, like this one, saying "the object cannot be called", most likely you used the built-in name as a variable in your code. In this case, the fix is โโas simple as renaming a variable. For example, to fix the above code, we could rename our list variable to ints :
ints = [1, 2, 3, 4, 5] # Rename "list" to "ints" myrange = list(range(1, 10)) for number in ints: # Renamed "list" to "ints" if number in myrange: print(number, 'is between 1 and 10')
PEP8 , the official Python style guide, contains many guidelines for naming variables.
This is a very common mistake made by new and old Python users. This is why it is important to always avoid using inline names as variables like str , dict , list , range , etc.
Many linters and IDEs warn you when you try to use the built-in name as a variable. If you often make this mistake, it may be worth the time to buy one of these programs.
I have not renamed the built-in name, but I still get โTypeError: the list object is not being called.โ What gives?
Another common cause of the above error is an attempt to index the list using parentheses ( () ) rather than square brackets ( [] ). For instance:
>>> lst = [1, 2] >>> lst(0) Traceback (most recent call last): File "<pyshell#32>", line 1, in <module> lst(0) TypeError: 'list' object is not callable
For an explanation of the complete problem and what you can do to fix it, see TypeError: the list object is not called when trying to access the list .