I get a TypeError: 'list' object not callable. "How to fix this error?

I have a simple script:

list = [1, 2, 3, 4, 5] myrange = list(range(1, 10)) for number in list: if number in myrange: print(number, 'is between 1 and 10') 

However, when I try to run my script, Python throws an error:

 Traceback (most recent call last): File "python", line 2, in <module> TypeError: 'list' object is not callable 

What does this error mean? Why am I getting it? And how can I fix this?

+9
source share
3 answers

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 .

+14
source

You have redefined the built-in Python list method.

 mylist = [1, 2, 3, 4, 5] # just use any other name than `list` 
+1
source

Here is the mcve !

 >>> []() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'list' object is not callable 

Try also {}() and ()() . The message TypeError: 'X' object is not callable means that you wrote expression(some_arguments) , where expression is an instance of type X , and this type does not support the use of function calls in the syntax. Most of the time you wrote this because you thought expression a function or other type of call.

+1
source

Source: https://habr.com/ru/post/1270973/


All Articles