1. 程式人生 > >【Python】'str' object is not callable/'int' object is not callable

【Python】'str' object is not callable/'int' object is not callable

‘str’ object is not callable/‘int’ object is not callable

Python報錯TypeError: ‘str’ object is not callable
類似的還有
‘int’ object is not callable

str='i'
str(1)  # TypeError: 'str' object is not callable
str =1
str(2)  # TypeError: 'int' object is not callable

也就是說
當內部函式被用作變數名後再呼叫該內部函式的時候會出現此錯誤

出現報錯 XXX is not callable的時候,很有可能是呼叫了被重新定義過的內建函式。

c_list = [1,2,3,3]
remove_duplicate  = set(c_list)
print(remove_duplicate)  # {1, 2, 3}
remove_duplicate_list = list(remove_duplicate)
print(remove_duplicate_list)  # [1, 2, 3]

上面這個list的去重是沒問題的,可以正常使用:

list = 1
c_list = list({1,2})
print(c_list)

報錯資訊:

Traceback (most recent call last):
  File "E:/Pycharm/tcp/isnotcallable.py", line 13, in <module>
    c_list = list({1,2})
TypeError: 'int' object is not callable

與上面的str一致.

所以需要特別在意不要使用Python自身本來有的函式作為變數