str( )是python自带函数,是python保留的关键字,定义变量时应该避免使用str作为变量名。如果在使用str( )函数之前已经定义过str变量,则会出现TypeError: ‘str’ object is not callable这个报错。
错误示例如下:
def demo():
str = """<?xml version="1.0" encoding="GB2312"?>
<tree>
<tree curCode="640000" text="宁夏局本部" attrsName="" attrs="" attrSplit="@#$" />
<tree curCode="650000" text="新疆局本部" attrsName="" attrs="" attrSplit="@#$" />
</tree>"""
i = 1
print("QualityInspectionBureau" + str(i))
当在Python中遇到'TypeError: 'str' object is not callable'错误时,通常是由于不小心将字符串变量命名为内置的str函数,导致调用冲突。避免该问题的方法是在定义变量时不要使用Python的保留关键字。
1万+

被折叠的 条评论
为什么被折叠?



