修復 Python 中不可呼叫的錯誤列表物件

修復 Python 中不可呼叫的錯誤列表物件

型別錯誤是 Python 程式中一些常見的標準異常。它們通常是由於未能遵守正確的語法或對不受支援的資料型別進行的操作造成的。

當我們試圖呼叫一個不可呼叫的物件時,經常會出現這個錯誤,就像我們呼叫一個普通的函式物件一樣。由於違反了正確的 python 語法,下面的程式碼片段返回一個 TypeError。

nums = [23, 34, 56, 67]
nums()

輸出:

Traceback (most recent call last):
  File "<string>", line 2, in <module>
TypeError: 'list' object is not callable

如果後面的一對括號可以觸發物件的執行,則物件被認為是可呼叫的,就像函式一樣。

幸運的是,Python 標準還提供了 callable() 函式,如果物件出現可呼叫則返回 True,如果物件不可呼叫則返回 False

在列表上方的示例中,物件是不可呼叫的,因此我們肯定會得到 False。

nums = [23, 34, 56, 67]
print(callable(nums))

輸出:

False

函式、方法和類在 Python 中是可呼叫的。

這是因為可以使用執行運算子呼叫它們的執行。Python 中的可呼叫物件列表包括 lambda 函式和自定義的可呼叫物件。

另一方面,元組、列表和字典等資料型別是不可呼叫的。因此,任何將它們作為 Python 中的普通函式或方法執行的嘗試都會導致 TypeError: object is not callable

當使用括號而不是方括號從列表中索引元素時,也可能會出現 TypeError list object not callable

在大多數程式語言中,方括號被認為是預設的索引運算子。然而,在編寫程式時,我們可能經常發現自己使用括號代替方括號,因為它們非常相似。

下面的程式碼片段旨在返回列表中索引 2 處的元素。

cars = ["Mazda", "Toyota", "BMW", "Tesla", "Hyundai"]
print(cars(2))

輸出:

Traceback (most recent call last):
  File "<string>", line 2, in <module>
TypeError: 'list' object is not callable

雖然新手經常犯這種錯誤,但即使是經驗豐富的開發人員,這也是常見的語法錯誤。這個錯誤可以通過簡單地使用方括號而不是括號來索引元素來解決,如下所示。

cars = ["Mazda", "Toyota", "BMW", "Tesla", "Hyundai"]
print(cars[2])

輸出:

BMW

在使用列表推導來縮短 Python 語法時,可能會出現使用括號而不是方括號來執行索引的情況。這是因為列表推導涉及多個方括號和圓括號的組合,而不是通常的 Python 語法。

在下面的示例中,括號在構造列表推導時被濫用。

top_companies = [
    ["microsoft", "apple", "ibm"],
    ["tesla", "lucid", "nikola"],
    ["foxcon", "huawei", "tencent"],
]
result = [[row(index).upper() for index in range(len(row))] for row in top_companies]
print(result)

輸出:

Traceback (most recent call last):
  File "<string>", line 2, in <module>
  File "<string>", line 2, in <listcomp>
  File "<string>", line 2, in <listcomp>
TypeError: 'list' object is not callable

上面的示例打算使用元素的索引遍歷巢狀列表並將其元素轉換為大寫。為了解決這個錯誤,我們需要檢查程式碼並確保我們使用方括號來索引,如下所示。

top_companies = [
    ["microsoft", "apple", "ibm"],
    ["tesla", "lucid", "nikola"],
    ["foxcon", "huawei", "tencent"],
]
result = [[row[index].upper() for index in range(len(row))] for row in top_companies]
print(result)

輸出:

[['MICROSOFT', 'APPLE', 'IBM'], ['TESLA', 'LUCID', 'NIKOLA'], ['FOXCON', 'HUAWEI', 'TENCENT']]

TypeError: list object is not callable 也可能在使用預定義名稱命名變數時遇到。一些最常被誤用的可能導致此類錯誤的內建名稱包括: strdictlistrange

在 Python 中,列表建構函式 list() 用於建立新列表。由於這是一個預定義的內建名稱和一個表示 Python 列表的類物件,因此將名稱列表用作變數名並不好。

使用名稱列表來命名變數可能會導致錯誤列表物件不可呼叫,如下例所示。

list = [24, 24, 25, 26, 28, 56]

nums_range = list(range(20, 40))

for number in list:
    if number in nums_range:
        print(number, "is the range")
    else:
        print(number, "number is not in range")

輸出:

Traceback (most recent call last):
  File "<string>", line 3, in <module>
TypeError: 'list' object is not callable

在上面的示例中,我們使用了預定義的名稱列表作為變數名稱。我們嘗試使用與建構函式相同的名稱在第二行建立一個新列表。

由於我們已經將此名稱用作變數名,因此 Python 將第二行解釋為嘗試呼叫列表物件,從而導致錯誤。

可以通過將列表物件重新命名為不同的名稱來解決上述錯誤。新名稱不應該是關鍵字,因為它確保 list() 建構函式保留其功能屬性。

nums = [23, 24, 25, 28, 27, 35, 78]

nums_range = list(range(20, 40))

for number in nums:
    if number in nums_range:
        print(number, "is the range")
    else:
        print(number, "is not in the range")

輸出:

23 is the range
24 is the range
25 is the range
28 is the range
27 is the range
35 is the range
78 is not in the range
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
作者: Isaac Tony
Isaac Tony avatar Isaac Tony avatar

Isaac Tony is a professional software developer and technical writer fascinated by Tech and productivity. He helps large technical organizations communicate their message clearly through writing.

LinkedIn

相關文章 - Python List

相關文章 - Python Object

相關文章 - Python Error