图片
尝试简化我的问题,所以使用 a、b 和 c 代替。
即使我复制了教程的答案并通过 Colaboratoy 运行它,它仍然显示 TypeError:'str' object is not callable。
这是为什么?
谢谢你。
回答问题标题:
如何修复 TypeError: 'str' 对象不可调用?
作为 python 的初学者,我觉得有必要回答这个问题,因为 python 和编辑器(至少带有 python 插件的 vscode)似乎都暗示你可能不小心用 fieldName 遮盖了 methodName。
考虑以下因素:
class ValueWrapper:
def __init__(self):
self.value = 'someValue'
def value(self):
return self.value
print("value:", ValueWrapper().value())
输出:类型错误:“str”对象不可调用
class ValueWrapper:
def __init__(self):
self.value = 'someValue'
def getValue(self):
return self.value
print("value:", ValueWrapper().getValue())
输出:值:someValue
print = "sample"
。因此,重新启动会话或执行
del print
命令并尝试执行您的代码。
del print
a=1
b=2
c=a+b
print(c)
尝试执行上面的代码,在Python中命名变量时也遵循以下规则。
# Valid identifier names
_private
_private123
myVariable
myVariable123
MY_CONSTANT
# Invalid identifier names
123invalid # cannot start with a digit
for # for is a reserved word