当前位置:网站首页>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
边栏推荐
- Leetcode skimming: dynamic programming 07 (different binary search trees)
- 前夕 - 0day威胁情报
- Improvement of wechat applet 29 hot search list ②
- [919. Complete binary tree inserter]
- How to ensure the consistency of double write between database and cache?
- Pymoo学习 (7):并行化Parallelization
- 【刷题记录】21. 合并两个有序链表
- Intouch高级报警(报警筛选)
- Wechat campus maintenance application applet graduation design finished product of applet completion work (8) graduation design thesis template
- Full scale and Xuan of C key
猜你喜欢

房企打响“保交战”

Imeta | sangerbox: interactive integrated clinical information analysis platform

虹科分享|如何解决勒索软件安全漏洞

【HDLBits 刷题】Verilog Language(3)Modules: Hierarchy 部分
![[encryption weekly] has the encryption market recovered? The cold winter has not thawed yet! Check the major events in the encryption market last week!](/img/6d/b037208996ce52016d014062deaa1f.jpg)
[encryption weekly] has the encryption market recovered? The cold winter has not thawed yet! Check the major events in the encryption market last week!

Fruit chain "siege": it's a journey of sweetness and bitterness next to apples

InTouch advanced alarm (alarm filtering)

二叉树可视化

Wechat campus maintenance application applet graduation design finished product of applet completion work (3) background function

常用的开发软件下载地址
随机推荐
Pymoo学习 (5):收敛性分析
聊聊接口性能优化的11个小技巧
“未来杯”第二届知识图谱锦标赛正式启动
How to change the chords after the tune of the song is changed
Small program completion work wechat campus maintenance application small program graduation design finished product (2) small program function
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 1)
Pymoo learning (8):grades
基于FPGA的1080P 60Hz BT1120接口调试过程记录
【919. 完全二叉树插入器】
How to ensure the consistency of double write between database and cache?
qt exec和show的区别
[Detr for 3D object detection] detr3d: 3D object detection from multi view images via 3D-to-2D queries
Wechat campus maintenance and repair applet graduation design finished product (5) assignment of applet completion work
Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
Wechat campus maintenance and repair application applet graduation design finished product of applet completion work (6) opening defense ppt
[web technology] 1391 page visualization building tool, previous life and present life
PHP等于==和恒等于===的区别
Pymoo learning (5): convergence analysis
JS learning notes 16: switching pictures small project practice
哈希无向图可视化