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

@本文来源于公众号:csdn2299,喜欢可以关注公众号 程序员学府
这篇文章主要介绍了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) 
<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函数

扫描二维码关注公众号,回复: 10578396 查看本文章

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

非常感谢你的阅读
大学的时候选择了自学python,工作了发现吃了计算机基础不好的亏,学历不行这是
没办法的事,只能后天弥补,于是在编码之外开启了自己的逆袭之路,不断的学习python核心知识,深入的研习计算机基础知识,整理好了,如果你也不甘平庸,那就与我一起在编码之外,不断成长吧!
其实这里不仅有技术,更有那些技术之外的东西,比如,如何做一个精致的程序员,而不是“屌丝”,程序员本身就是高贵的一种存在啊,难道不是吗?[点击加入]想做你自己想成为高尚人,加油!

发布了10 篇原创文章 · 获赞 0 · 访问量 2794

猜你喜欢

转载自blog.csdn.net/chengxun03/article/details/105375869