Matt Phillips Matt Phillips - 2 months ago 5x
Python Question

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

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'>


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!-)