为什么在使用sum()函数时出现“ int”对象不可调用错误?

为什么在使用sum()函数时出现“ int”对象不可调用错误?,第1张

为什么在使用sum()函数时出现“ int”对象不可调用错误

您可能将“ sum”函数重新定义为整数数据类型。因此,它正确地告诉您,整数不能传递范围。

要解决此问题,请重新启动解释器。

Python 2.7.3 (default, Apr 20 2012, 22:44:07) [GCC 4.6.3] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> data1 = range(0, 1000, 3)>>> data2 = range(0, 1000, 5)>>> data3 = list(set(data1 + data2)) # makes new list without duplicates>>> total = sum(data3) # calculate sum of data3 list's elements>>> print total233168

如果您隐藏了

sum
内置函数,则会得到您看到的错误

>>> sum = 0>>> total = sum(data3) # calculate sum of data3 list's elementsTraceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: 'int' object is not callable

另外,请注意,该方法

sum
可以很好地工作,
set
无需将其转换为
list



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5655239.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存