How to Solve Python TypeError: ‘list’ object is not callable

What will happen if the list variable and the list function have the same name?We can refer to the following code:

list = ['Puff', 'Sourdough', 'Fish', 'Camel']

tup_1 = (1, 2, 3, 4, 5)
tupToList = list(tup_1)

print(tupToList)

An error occurred after the code was run. The reason for the error is typeerror: ‘list’ object is not called

Traceback (most recent call last):
  File "D:/python_workshop/python6/lesson3_list.py", line 6, in <module>
    tupToList = list(tup_1)
TypeError: 'list' object is not callable

Callable () is a built-in function of python, which is used to check whether an object can be called. Callable refers to whether an object can be called with () brackets

In the above code, because the variable list and function list have the same name, when the function uses the list function, it finds that the list is a well-defined list, and the list cannot be called, so it throws a type error

Solution: we just need to modify the variable name list

list_1 = ['Puff', 'Sourdough', 'Fish', 'Camel']

tup_1 = (1, 2, 3, 4, 5)
tupToList = list(tup_1)

print(tupToList)

After operation and the results are normal

[1, 2, 3, 4, 5]

Therefore, when naming variables, you should avoid conflicts with Python function names and keywords

Similar Posts: