gpt4 book ai didi

python - 官方 Python 教程(第 9.10 章)中的错误示例?

转载 作者:行者123 更新时间:2023-11-28 21:41:51 24 4
gpt4 key购买 nike

我将以下示例从Python 3.6.1 文档,第 9.10 章复制到 Jupyter Notebook(Python 版本 3.6.1)中:

xvec = [10, 20, 30]

yvec = [7, 5, 3]

sum(x*y for x,y in zip(xvec, yvec)) # dot product

虽然官方文档说它会打印260,但我在笔记本中得到了以下错误:

----> 4 sum(x*y for x,y in zip(xvec, yvec))
TypeError: 'int' object is not callable

这绝对不仅仅是关于“int object is not callable”的问题,更多的是关于被认为是 Python 福音的明显错误。

最佳答案

如果您错误地覆盖了zip() 或/和sum(),这会导致您当前的代码无法运行。您可以使用 del 恢复它们的默认功能,就像这个例子:

>>> zip = [1]
>>> zip
[1]
>>> del zip
>>> zip
<function zip>

所以,你可以试试:

>>> del zip
>>> del sum
>>> xvec = [10, 20, 30]
>>> yvec = [7, 5, 3]
>>> sum(x*y for x,y in zip(xvec, yvec))

它会输出:

260

关于python - 官方 Python 教程(第 9.10 章)中的错误示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44351221/

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