为什么这会给我一个错误?
>>> variable = str(21)
Traceback (most recent call last):
File "<pyshell#101>", line 1, in <module>
variable = str(21)
TypeError: 'str' object is not callable
答案 0 :(得分:37)
单独的代码不会给你一个错误。例如,我刚试过这个:
~ $ python3.2
>>> variable = str(21)
>>> variable
'21'
在代码中的某个位置,您要定义str =
其他内容,屏蔽str
的内置定义。删除它,你的代码将正常工作。
答案 1 :(得分:13)
因为您可能通过调用自己的变量str
来覆盖str
函数。