当前位置:网站首页>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
边栏推荐
- 微信小程序 27 进度条的动态实现和搜索框、热搜榜的静态搭建
- [encryption weekly] has the encryption market recovered? The cold winter has not thawed yet! Check the major events in the encryption market last week!
- 某公司网络设计与规划
- Sccm2012r2 network deployment reinstallation system
- [cloud native kubernetes] management of secret storage objects under kubernetes cluster
- 哈希无向图可视化
- The difference between PHP equal to = = and identity equal to = = =
- The difference between QT exec and show
- 英诚医院内部网络规划与设计
- 有孚网络受邀参加2022全国CIO大会并荣获“CIO信赖品牌”称号
猜你喜欢

Solve the problem that the win10 account has no administrator rights

Sccm2012r2 network deployment reinstallation system
![[record of question brushing] 21. Merge two ordered linked lists](/img/81/5f1ab0054ef5e8cba32f7a546a64c4.png)
[record of question brushing] 21. Merge two ordered linked lists

HTTP cache tongtianpian, there may be something you want

小程序毕设作品之微信校园维修报修小程序毕业设计成品(4)开题报告

iMeta | Sangerbox: 交互式整合临床生信分析平台

前夕 - 0day威胁情报

JS learning notes 17: DOM query exercise

The second "future Cup" knowledge map championship was officially launched

歌曲转调之后和弦如何转换
随机推荐
【刷题记录】21. 合并两个有序链表
[Detr for 3D object detection] 3detr: an end to end transformer model for 3D object detection
小程序毕设作品之微信校园维修报修小程序毕业设计成品(6)开题答辩PPT
Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
鸿蒙-大喵计算画板-视频
【小程序开发】常用组件及基本使用详解
Wechat campus maintenance application applet graduation design finished product of applet completion work (8) graduation design thesis template
解决Win10账户没有了管理员权限
Clip can also do segmentation tasks? The University of Gottingen proposed a model clipseg that uses text and image prompt and can do three segmentation tasks at the same time, squeezing out the clip a
InTouch advanced alarm (alarm filtering)
Juzhi cloud computing opens a new era to the "proprietary cloud" of Youfu network
微信小程序 29 热搜榜的完善②
Huawei switch system software upgrade and security vulnerability repair tutorial
【DETR用于3D目标检测】DETR3D: 3D Object Detection from Multi-view Images via 3D-to-2D Queries
【HDLBits 刷题】Verilog Language(3)Modules: Hierarchy 部分
无惧高温暴雨,有孚网络如何保您无忧?
基础乐理之音程的度数
哈希无向图可视化
Fearless of high temperature and rainstorm, how can Youfu network protect you from worry?
【小程序开发】你了解小程序开发吗?