Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'int' object is not callable when using the sum function on a list [duplicate]

Tags:

coinCount = [2 for i in range(4)] total = sum(coinCount) 

This gives me

TypeError: 'int' object is not callable 

I don't understand why because

print type(coinCount) 

Gives me

type <'list'> 
like image 321
Matt Phillips Avatar asked Mar 17 '10 05:03

Matt Phillips


People also ask

How do I fix int object is not callable in Python?

But in Python, this would lead to the Typeerror: int object is not callable error. To fix this error, you need to let Python know you want to multiply the number outside the parentheses with the sum of the numbers inside the parentheses. Python allows you to specify any arithmetic sign before the opening parenthesis.

How do I fix list object is not callable?

The Python "TypeError: 'list' object is not callable" occurs when we try to call a list as a function using parenthesis () . To solve the error, make sure to use square brackets when accessing a list at a specific index, e.g. my_list[0] .

How do you use sum in Python?

How to find a sum of the List in Python. To find a sum of the List in Python, use the sum() method. The sum() is a built-in method that is used to get the summation. You need to define the List and pass the List as a parameter to the sum() function, and you will get the sum of list items in return.

How do you make an int object Subscriptable?

The TypeError: 'int' object is not subscriptable error occurs if we try to index or slice the integer as if it is a subscriptable object like list, dict, or string objects. The issue can be resolved by removing any indexing or slicing to access the values of the integer object.


1 Answers

You no doubt used the identifier sum previously in your code as a local variable name, and the last value you bound to it was an int. So, in that code snippet, you're trying to call the int. print sum just before you try calling it and you'll see, but code inspection will probably reveal it faster.

This kind of problem is why expert Pythonistas keep telling newbies over and over "don't use builtin names for your own variables!" even when it's apparently not hurting a specific code snippet: it's a horrible habit, and a bug just waiting to happen, if you use identifiers such as sum, file, list, etc, as your own variables or functions!-)

like image 146
Alex Martelli Avatar answered Sep 28 '22 08:09

Alex Martelli