Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python3中调用str()将整数转换为字符串的古怪行为?_Python_Python 3.x_Shadowing - Fatal编程技术网

在Python3中调用str()将整数转换为字符串的古怪行为?

在Python3中调用str()将整数转换为字符串的古怪行为?,python,python-3.x,shadowing,Python,Python 3.x,Shadowing,为什么这会给我一个错误 >>> variable = str(21) Traceback (most recent call last): File "<pyshell#101>", line 1, in <module> variable = str(21) TypeError: 'str' object is not callable 变量=str(21) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 变量=str(21) T

为什么这会给我一个错误

>>> variable = str(21)

Traceback (most recent call last):
  File "<pyshell#101>", line 1, in <module>
    variable = str(21)
TypeError: 'str' object is not callable
变量=str(21) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 变量=str(21) TypeError:“str”对象不可调用
因为您可能通过调用自己的变量
str
覆盖了
str
函数,所以仅此代码不会给出错误。例如,我刚刚尝试了以下方法:

~ $ python3.2
>>> variable = str(21)
>>> variable
'21'

您在代码的某个地方定义了
str=
其他内容,屏蔽了
str
的内置定义。删除它,代码就会正常工作。

是否将变量命名为“str”?是否定义了另一个字符串变量并将其分配给变量
str
?因为这样做最终会隐藏内置函数
str()
例如:
str='test';打印(str(124))
。否决票放错了位置。我们中的大多数人在学习时都会对内置设备进行跟踪。看看有多少带有
list=[…]
的代码示例。我更新了这个问题的标题。我想你可能是对的。我关闭了空闲会话(我发现在线加载了很多早期的代码示例),现在我可以正确使用内置的str函数了。其中一个早期的代码示例肯定对BIF str()做了一些奇怪的操作。谢谢