Python3_TypeError: 'list' object is not callable
阿新 • • 發佈:2019-01-05
在Python執行過程中遇到了如下錯誤:
TypeError: ‘list’ object is not callable
list = ['經點', '鹹湯', '魚兒', '駱駝']
tup_1 = (1, 2, 3, 4, 5)
tupToList = list(tup_1)
print(tupToList)
- 1
- 2
- 3
- 4
- 5
- 6
程式碼執行後出錯了,提示是TypeError: ‘list’ object is not callable
Traceback (most recent call last): File "<pyshell#42>", line 1, in <module> counterA() TypeError: 'list' object is not callable
- 1
- 2
- 3
- 4
callable()是python的內建函式,用來檢查物件是否可被呼叫,可被呼叫指的是物件能否使用()括號的方法呼叫,類似於iterable()
在如上程式碼中,由於變數list和函式list重名了,所以函式在使用list函式時,發現list是一個定義好的列表,而列表是不能被呼叫的,因此丟擲一個型別錯誤
解決辦法
我們只需修改變數名listx就可以了:
listx = ['經點', '鹹湯', '魚兒', '駱駝'] tup_1 = (1, 2, 3, 4, 5) tupToList = list(tup_1) print(tupToList)
- 1
- 2
- 3
- 4
- 5
- 6
執行後和結果是正常的:
[1, 2, 3, 4, 5]
因此,在命名變數時要注意,應避免和python的函式名、關鍵字衝突。