27
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'>
Matt Phillips
  • 11,249
  • 10
  • 46
  • 71

2 Answers2

112

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

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 4
    A frequently recommended alternative is to put an underscore postfix on identifiers that would otherwise collide with reserved words and builtin identifiers; i.e. `type_` or `sum_` or `from_` (if you're into using prepositions as identifiers). – cdleary Mar 17 '10 at 07:21
  • @cdleary very sound advice. I prefer this over prefixing with an underscore. – XMAN Feb 23 '22 at 17:28
15

I'd like to add on to what Alex Martelli said by saying don't use sum as a variable name but if you're program is too long already or you're using idle and you don't want to start over you can do

sum2 = sum
del sum

to transfer the value of sum into sum2 (if necessary) and delete the variable sum (allowing you to continue working with the sum function)

Nick the coder
  • 309
  • 3
  • 8