如何修复 TypeError: 'str' 对象不可调用?

问题描述 投票:0回答:2

图片

1

尝试简化我的问题,所以使用 a、b 和 c 代替。

即使我复制了教程的答案并通过 Colaboratoy 运行它,它仍然显示 TypeError:'str' object is not callable。

这是为什么?

谢谢你。

string typeerror callable
2个回答
0
投票

回答问题标题:

如何修复 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


-1
投票
您可能在某个地方将 print 声明为变量。例如:

print = "sample"

。因此,重新启动会话或执行 
del print
 命令并尝试执行您的代码。

del print a=1 b=2 c=a+b print(c)
尝试执行上面的代码,在Python中命名变量时也遵循以下规则。

    标识符必须以字母或下划线 (_) 开头。
  • 标识符可以包含字母、数字和下划线。
  • 标识符不能是保留字。
以下是 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
    
© www.soinside.com 2019 - 2024. All rights reserved.