tags:

views:

49

answers:

1
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'>
+5  A: 

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