【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自身本来有的函数作为变量

猜你喜欢

转载自blog.csdn.net/lvluobo/article/details/81088633