Python中的str is not callable问题分析
来源:互联网 发布:deandre ayton体测数据 编辑:程序博客网 时间:2024/05/21 17:44
问题提出:
在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)<ipython-input-44-02ec8a351cce> in <module>()----> 1 check_province_code('ab', '001')<ipython-input-43-12db968aa80a> 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)<ipython-input-45-afcef5460e92> in <module>()----> 1 str(1233)TypeError: 'str' object is not callable这下问题定义清楚了,原来没有了str,仔细想了想原来刚才在定义变量的时候,随机使用str,所以就被覆盖了str函数。进行了类似以下的操作:
str = '123'
恢复默认的str函数
重新启动一下python应用,移除str被覆盖的代码部分即可。
总结
在python中内置了很多的函数和类,在自己定义变量的时候,切记不要覆盖或者和他们的名字重复。
0 0
- Python中的str is not callable问题分析
- 有关python:'str' object is not callable
- str' object is not callable
- python错误:TypeError: 'str' object is not callable
- Python报错TypeError: 'str' object is not callable
- Python报错TypeError: 'str' object is not callable
- Python报错TypeError: 'str' object is not callable
- TypeError: 'str' object is not callable
- TypeError: 'str' object is not callable
- TypeError: 'str' object is not callable
- TypeError: 'str' object is not callable
- TypeError: 'str' object is not callable
- TypeError: 'str' object is not callable
- Django错误'str' object is not callable
- TypeError: 'str' object is not callable
- TypeError: 'str' object is not callable
- TypeError: 'str' object is not callable
- TypeError: 'str' object is not callable
- 心灵小鸡汤
- cas源码改造-改造登录页面
- 通过shadowsocks上网,根据国内外IP自动选择是否需要走代理
- 关于Android数据库orm工具库对比的几点思考(四)
- NYOJ - 17 - 单调递增最长子序列(动态规划--LIS--单调递增最长子序列)
- Python中的str is not callable问题分析
- Mysql几种索引方式的区别及适用情况
- 策略模式
- HTML5本地储存
- 第1节--python课程介绍
- JPA project Change Event Handler问题解决
- 《干了这碗weex》入坑
- 《 iOS 应用逆向工程》作者沙梓社专访:运用逆向工程思维来优化自己的产品
- LeetCode-Maximum Depth of Binary Tree