“str"对象不可调用 - 注意:不要将特殊函数用作变量 [英] 'str' object is not callable - CAUTION: DO NO USE SPECIAL FUNCTIONS AS VARIABLES

查看:31
本文介绍了“str"对象不可调用 - 注意:不要将特殊函数用作变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

如果您定义了预定义类型,例如:str = 5 然后该预定义的原始功能将更改为新功能.经验教训:不要给预定义或属于特殊函数的变量命名.在这里列出:这里

在将其标记为重复之前,请查看此python代码和结果

X=5print("X 值为:"+str(X))

<块引用>

回溯(最近一次调用最后一次):文件",第 1 行,在print("X 值为:"+str(X))类型错误:str"对象不可调用*

据我所知, str 应该在这里工作,因为我只是在这里打印变量的值.有什么线索吗?

解决方案

问题是str函数一定是重载了.

输入:

X=5print("X 值为:"+str(X))

输出:X值为:5

输入:

X=5str = "98897"print("X 值为:"+str(X))

输出:TypeError Traceback(最近一次调用最后一次)在 ()----> 1 print("X 值为:"+str(X))

TypeError: 'str' 对象不可调用

EDIT: If you define a predefined type such as: str = 5 then the original functionality of that predefined will change to a new one. Lesson Learnt: Do not give variables names that are predefined or belong to special functions. List here: Here

Before you mark it as duplicate, please see this python code and results

X=5

print("X value is:"+str(X))

Traceback (most recent call last): File "", line 1, in print("X value is:"+str(X)) TypeError: 'str' object is not callable*

As far as I know, str should work here because I am simply printing the value of variable here. Any clues?

解决方案

The problem is str function must have been overloaded.

Input:

X=5
print("X value is:"+str(X))

Output: X value is:5

Input:

X=5
str = "98897"
print("X value is:"+str(X))

Output: TypeError Traceback (most recent call last) in () ----> 1 print("X value is:"+str(X))

TypeError: 'str' object is not callable

这篇关于“str"对象不可调用 - 注意:不要将特殊函数用作变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆