关于错误:'str' object is not callable可能的原因之一
来源:互联网 发布:网络交换机的技术参数 编辑:程序博客网 时间:2024/05/17 06:59
今天写代码遇到了一个问题TypeError: 'str' object is not callable。
给大家看看我的代码:(代码功能说明:用正则表达式匹配数字并对数字进行+1操作,然后替换原数字)
str='literal books=1000'def add1(match): val=match.group() num=int(val)+1 return str(num)info=re.sub(r'\d+',add1,str)print info#literal books=1001
为什么会出错呢?百度了一下,看到有个国外网站说“You are redefining whatstr()
means.str
is the built-in Python name of the string type, and you don't want to change it.Use a different name for the local variable, and remove theglobal
statement.”
翻译过来意思是,str()是系统自带的,你不能在用它的时候自己同时定义一个别的叫做str的变量,这样会冲突。
于是我把自定义的str变量改成了别的名字,str1,代码就通过了。
这是因为如果我自定义叫str的变量,str会被系统识别成字符串转换函数,这样的函数被传进sub函数的参数里面,is not callable,也就是说,是不可调用的。大家检查一下自己的代码是不是也有类似的错误呢?
希望有所帮助:)
-------------------2017.3.21更新
今天学习了 __call__函数。在Python中,函数其实是一个对象,并且所有的函数都是可调用对象。一个类实例也可以变成一个可调用对象,只需要实现一个特殊方式__call__().
所以,当出现报错 XXX is not callable的时候,很有可能是你正在调用一个不能被调用的变量或对象,具体表现就是你调用函数、变量的方式错误。
0 0
- 关于错误:'str' object is not callable可能的原因之一
- Django错误'str' object is not callable
- str' object is not callable
- python错误: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
- 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
- 有关python:'str' object is not callable
- python错误TypeError: 'module' object is not callable 的解决方法
- 基于Web的DBI开发
- 基于opencv的计算机视觉技术实现 第五章节第一个程序 改错
- 解决vs2015后端vshub.exe自动启动问题
- Android background背景图片平铺
- 用GDB调试程序(六)
- 关于错误:'str' object is not callable可能的原因之一
- 实现UITextField值变化的实时监视
- 淘宝卖家辅助工具有哪些比较好用的?
- 在linux环境下使用C++语言、rabbitMQ进行开发
- Broadcast Receiver
- iOS之ReactiveCocoa简介和使用(二):MVVM
- spoj QTREE3 Query on a tree again!
- PHP内核探索:哈希碰撞攻击是什么?
- 用GDB调试程序(七)