TypeError系列之:TypeError: 'tuple' object is not callable.
生活随笔
收集整理的這篇文章主要介紹了
TypeError系列之:TypeError: 'tuple' object is not callable.
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
TypeError: 'tuple' object is not callable。這又是一個typeerror。我們翻譯一下這個報錯信息,
tuple對象是不可調用的,這是什么意思。我看了網上很多的東西,發現根本沒有一個合理的解釋,于是就自己做了幾組實驗來分析一下到底是個什么情況:
實驗如下:
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)我們不難發現,a.shape是一個turple數據類型,你在后面加“()”,相當于把a.shape看成了一個函數名,a.shape(),相當于調用a.shape函數,因此會報錯:
tuple對象不能被調用 的錯誤!!!!
所以下次再出現這種問題,不管是int,list還是其他的,首先先看看自己的“[ ]” 和“()”是否用錯了,是否誤把數據類型當做函數調用。
一定要與另外一種錯誤區分:
AttributeError: 'tuple' object has no attribute 'shape'
詳見我的另一篇博客:https://blog.csdn.net/qq_41368074/article/details/105738815
?
總結
以上是生活随笔為你收集整理的TypeError系列之:TypeError: 'tuple' object is not callable.的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TypeError系列之:TypeErr
- 下一篇: AttributeError系列之:At