为什么这会给我一个错误?
>>> variable = str(21)
Traceback (most recent call last):
File "<pyshell#101>", line 1, in <module>
variable = str(21)
TypeError: 'str' object is not callable
为什么这会给我一个错误?
>>> variable = str(21)
Traceback (most recent call last):
File "<pyshell#101>", line 1, in <module>
variable = str(21)
TypeError: 'str' object is not callable
仅凭代码本身不会造成错误。例如,我刚刚尝试了这个:
~ $ python3.2
>>> variable = str(21)
>>> variable
'21'
你的代码中某处定义了str =
其他内容,掩盖了内置定义的str
。移除它后,你的代码将正常工作。
因为您可能通过将自己的变量命名为 str
来覆盖了内置的 str
函数。
str
?因为这样做会导致遮蔽内置函数str()
,例如:str = 'test'; print(str(124))
。 - mouadlist = [...]
。我更新了这个问题的标题。 - smci