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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值