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:
- Python uses decorator and reports error typeerror: ‘nonetype’ object is not callable ‘
- TypeError: ‘list’ object cannot be interpreted as an integer
- PaddlePaddle Error: ‘map’ object is not subscriptable
- Fast locating nonetype ‘object is not Iterable in Python
- [Solved] Python TypeError: ‘int’ object is not callable
- [Solved] Python TypeError: sequence item 0: expected str instance, int found
- [Solved] An error occurred when paddlepaddle iterated data: typeerror: ‘function’ object is not iterative
- [Solved] Python Error: TypeError: ‘dict_keys’ object does not support indexing
- Python3: list dict set [UNK]unhashable type
- Tensorflow gradients TypeError: Fetch argument None has invalid type