当前位置:网站首页>TypeError: ‘str‘ object is not callable的错误原因
TypeError: ‘str‘ object is not callable的错误原因
2022-07-25 19:21:00 【闪亮伞】
今天写代码遇到了一个问题TypeError: 'str' object is not callable。
错误原因1
给大家看看我的代码:(代码功能说明:用正则表达式匹配数字并对数字进行+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 what str()
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 the global statement.
翻译过来意思是,str()是系统自带的,你不能在用它的时候自己同时定义一个别的叫做str的变量,这样会冲突。
于是我把自定义的str变量改成了别的名字,str1,代码就通过了。
这是因为如果我自定义叫str的变量,str会被系统识别成字符串转换函数,这样的函数被传进sub函数的参数里面,is not callable,也就是说,是不可调用的。大家检查一下自己的代码是不是也有类似的错误呢?
错误原因2
在Python中,函数其实是一个对象,并且所有的函数都是可调用对象。一个类实例也可以变成一个可调用对象,只需要实现一个特殊方式__call__().
所以,当出现报错 XXX is not callable的时候,很有可能是你正在调用一个不能被调用的变量或对象,具体表现就是你调用函数、变量的方式错误。
原文链接:https://blog.csdn.net/lifelegendc/article/details/55051374
边栏推荐
- 基础乐理--配置和弦
- Real estate enterprises have launched a "war of guarantee"
- 【DETR用于3D目标检测】DETR3D: 3D Object Detection from Multi-view Images via 3D-to-2D Queries
- The difference between QT exec and show
- Juzhi cloud computing opens a new era to the "proprietary cloud" of Youfu network
- 小程序毕设作品之微信校园维修报修小程序毕业设计成品(7)中期检查报告
- Improvement of wechat applet 29 hot search list ②
- Improvement of wechat applet 28 hot search list ①
- ERROR: role “admin“ cannot be dropped because some objects depend on itDETAIL:
- Swift 基础 Codable(JSONEncoder JSONDecoder)的使用
猜你喜欢

小程序毕设作品之微信校园维修报修小程序毕业设计成品(3)后台功能

Empire CMS whole station | mobile number /qq lianghao mall source code | suitable for mobile terminal
![[hdlbits questions] Verilog language (3) modules: hierarchy section](/img/35/ccdbb55aa0aff7e9dec2bf9e64c4e2.png)
[hdlbits questions] Verilog language (3) modules: hierarchy section
![[Detr for 3D object detection] 3detr: an end to end transformer model for 3D object detection](/img/22/426bcb8641db5bfe07e8aacf5e8427.png)
[Detr for 3D object detection] 3detr: an end to end transformer model for 3D object detection

HTTP cache tongtianpian, there may be something you want

基础乐理之音程的度数

帝国CMS整站|手机号/QQ靓号商城源码|适配移动端

某公司网络设计与规划

解决Win10账户没有了管理员权限

二叉树可视化
随机推荐
Full scale and Xuan of C key
How to be a self disciplined person?
新瓶装老酒--近期APT32(海莲花)组织攻击活动样本分析
【阅读笔记】《深度学习》第一章:引言
PyQt5单击QTableView垂直表头verticalHeader获取行数据以及单击单元格获取行数据操作
【小程序开发】宿主环境详解
CLIP还能做分割任务?哥廷根大学提出一个使用文本和图像prompt,能同时作三个分割任务的模型CLIPSeg,榨干CLIP能力...
Real estate enterprises have launched a "war of guarantee"
聚智云算,向新而生| 有孚网络“专有云”开启新纪元
leetcode刷题:动态规划07(不同的二叉搜索树)
qt之编译成功但程序无法运行
Pymoo学习 (7):并行化Parallelization
阿里云免费SSL证书申请详细流程
微信小程序 26 播放音乐页的完善②
Talk about 15 tips of SQL optimization
[Detr for 3D object detection] 3detr: an end to end transformer model for 3D object detection
基于PHP的中非南南合作信息交流平台网站建设
基于FPGA的1080P 60Hz BT1120接口调试过程记录
“未来杯”第二届知识图谱锦标赛正式启动
[record of question brushing] 21. Merge two ordered linked lists