python callable错误_Python中str is not callable问题详解及解决办法

Python中str is not callable问题详解及解决办法

问题提出:

在Python的代码,在运行过程中,碰到了一个错误信息:

python代码:

def check_province_code(province, country):

num = len(province)

while num <3:

province = ''.join([str(0),province])

num = num +1

return country + province

运行的错误信息:

check_province_code('ab', '001')

---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

in ()

----> 1 check_province_code('ab', '001')

in check_province_code(province, country)

3

4 while num <3:

----> 5 province = ''.join([str(0),province])

6 num = num +1

7

TypeError: 'str' object is not callable

问题分析与排查:

从错误信息分析, str不是一个可调用的对象,可是之前确实可以调用的,且在python的api文档中,其是python内置的一个函数呀, 怎么不能用了呢?

还是继续验证一下吧。

在命令行下执行str(123),将数字转换为string:

>>> str(1233)

---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

in ()

----> 1 str(1233)

TypeError: 'str' object is not callable

这下问题定义清楚了,原来没有了str,仔细想了想原来刚才在定义变量的时候,随机使用str,所以就被覆盖了str函数。进行了类似以下的操作:

str = '123'

恢复默认的str函数

重新启动一下python应用,移除str被覆盖的代码部分即可。

总结

在python中内置了很多的函数和类,在自己定义变量的时候,切记不要覆盖或者和他们的名字重复。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

本文标题: Python中str is not callable问题详解及解决办法

本文地址: http://www.cppcns.com/jiaoben/python/178879.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值