为什么在使用 sum() 函数时会出现“int"对象不可调用错误? [英] Why does the 'int' object is not callable error occur when using the sum() function?

查看:105
本文介绍了为什么在使用 sum() 函数时会出现“int"对象不可调用错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚为什么在范围内使用 sum 函数时会出错.

代码如下:

data1 = range(0, 1000, 3)数据 2 = 范围(0、1000、5)data3 = list(set(data1 + data2)) # 生成没有重复的新列表total = sum(data3) # 计算data3列表元素的总和打印总数

这里是错误:

第 8 行,在  中total2 = sum(data3)类型错误:int"对象不可调用

我找到了错误的解释:

<块引用>

在 Python 中,可调用"通常是一个函数.该消息意味着您将一个数字(一个 >int")视为一个函数(一个可调用"),因此 Python 不知道该做什么,所以它 > 停止了.

我还了解到 sum() 可以用于列表,所以我想知道这里出了什么问题?

我刚刚在 IDLE 模块中尝试过,效果很好.但是,它在 python 解释器中不起作用.关于如何做到这一点的任何想法?

解决方案

您可能将sum"函数重新定义为整数数据类型.所以它正确地告诉你整数不是你可以传递范围的东西.

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

Python 2.7.3(默认,2012 年 4 月 20 日,22:44:07)[GCC 4.6.3] 在 linux2 上输入帮助"、版权"、信用"或许可"以获取更多信息.>>>数据 1 = 范围(0、1000、3)>>>数据 2 = 范围(0、1000、5)>>>data3 = list(set(data1 + data2)) # 生成没有重复的新列表>>>total = sum(data3) # 计算data3列表元素的总和>>>打印总数233168

如果你隐藏了 sum 内置,你会得到你看到的错误

<预><代码>>>>总和 = 0>>>total = sum(data3) # 计算data3列表元素的总和回溯(最近一次调用最后一次):文件<stdin>",第 1 行,位于 <module>类型错误:int"对象不可调用

另外,请注意 sum 可以在 set 上正常工作,无需将其转换为 list

I'm trying to figure out why I'm getting an error when using the sum function on a range.

Here is the code:

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 total

And here is the error:

line 8, in <module> total2 = sum(data3)
TypeError: 'int' object is not callable

I found this explanation for the error:

In Python a "callable" is usually a function. The message means you are treating a number (an >"int") as if it were a function (a "callable"), so Python doesn't know what to do, so it >stops.

I've also read that sum() is capable of being used on lists, so I'm wondering what is going wrong here?

I just tried it in an IDLE module and it worked fine. However, it doesn't work in the python interpreter. Any ideas on how that can be?

解决方案

You probably redefined your "sum" function to be an integer data type. So it is rightly telling you that an integer is not something you can pass a range.

To fix this, restart your interpreter.

Python 2.7.3 (default, Apr 20 2012, 22:44:07) 
[GCC 4.6.3] on linux2
Type "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 total
233168

If you shadow the sum builtin, you can get the error you are seeing

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

Also, note that sum will work fine on the set there is no need to convert it to a list

这篇关于为什么在使用 sum() 函数时会出现“int"对象不可调用错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆