当前位置:网站首页>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
边栏推荐
- 有孚原力超算,为客户提供定制化高性能计算服务
- MES管理系统有什么应用价值
- QT compiled successfully, but the program could not run
- [web technology] 1391 page visualization building tool, previous life and present life
- 某公司网络设计与规划
- The second "future Cup" knowledge map championship was officially launched
- [cloud native kubernetes] management of secret storage objects under kubernetes cluster
- Talk about 15 tips of SQL optimization
- Youfu network was invited to attend the 2022 national CIO conference and won the title of "CIO trusted brand"
- Alibaba cloud free SSL certificate application detailed process
猜你喜欢
随机推荐
微信小程序 29 热搜榜的完善②
The difference between QT exec and show
Improvement of wechat applet 26 playing music page ②
Basic music theory -- configuring chords
C 调的满级和玄
The second "future Cup" knowledge map championship was officially launched
[919. Complete binary tree inserter]
Single arm routing experiment demonstration (Huawei router device configuration)
[encryption weekly] has the encryption market recovered? The cold winter has not thawed yet! Check the major events in the encryption market last week!
哪吒 D1-H 测试 microbench
虹科分享|如何解决勒索软件安全漏洞
前夕 - 0day威胁情报
Istio exposes applications to the Internet
Pymoo学习 (6):终止条件
房企打响“保交战”
leetcode刷题:动态规划07(不同的二叉搜索树)
MES管理系统有什么应用价值
Pymoo学习 (8):Gradients
Wechat campus maintenance and repair applet graduation design finished product of applet completion work (4) opening report
How to be a self disciplined person?









