gpt4 book ai didi

python - 为什么这是类型错误而不是语法错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:41:05 25 4
gpt4 key购买 nike

为什么会有代码

print("Average =" (sum/count))

产生类型错误而不是语法错误,因为缺少逗号?

谢谢。

最佳答案

Python 将 () 视为函数调用;字符串不可调用,导致 TypeError:

>>> "somestring"(42)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

在 Python 中,一切 都是对象;函数也是对象,任何对象都可以实现 __call__ 方法,使每个对象都可以调用。 Python 直到运行时才知道字符串对象不可调用,因此这不是语法错误。

关于python - 为什么这是类型错误而不是语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20886618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com