TypeError系列之:TypeError: 'tuple' object is not callable.
TypeError: 'tuple' object is not callable。這又是一個typeerror。我們翻譯一下這個報錯信息,
tuple對象是不可調(diào)用的,這是什么意思。我看了網(wǎng)上很多的東西,發(fā)現(xiàn)根本沒有一個合理的解釋,于是就自己做了幾組實驗來分析一下到底是個什么情況:
實驗如下:
import numpy as npa = np.zeros([5,5]) print(a) print(type(a)) print(type(a.shape)) print(a.shape)輸出:
[[0. 0. 0. 0. 0.][0. 0. 0. 0. 0.][0. 0. 0. 0. 0.][0. 0. 0. 0. 0.][0. 0. 0. 0. 0.]] <class 'numpy.ndarray'> <class 'tuple'> (5, 5)我們不難發(fā)現(xiàn),a.shape是一個turple數(shù)據(jù)類型,你在后面加“()”,相當于把a.shape看成了一個函數(shù)名,a.shape(),相當于調(diào)用a.shape函數(shù),因此會報錯:
tuple對象不能被調(diào)用 的錯誤!!!!
所以下次再出現(xiàn)這種問題,不管是int,list還是其他的,首先先看看自己的“[ ]” 和“()”是否用錯了,是否誤把數(shù)據(jù)類型當做函數(shù)調(diào)用。
一定要與另外一種錯誤區(qū)分:
AttributeError: 'tuple' object has no attribute 'shape'
詳見我的另一篇博客:https://blog.csdn.net/qq_41368074/article/details/105738815
?
總結
以上是生活随笔為你收集整理的TypeError系列之:TypeError: 'tuple' object is not callable.的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TypeError系列之:TypeErr
- 下一篇: AttributeError系列之:At