0

Why does the following generate the TypeError: 'float' object not callable?

sum([-450.0,950.0])

2
  • Works fine for me in 3.3. >>> sum([-450.0, 950.0]) 500.0 Commented Feb 5, 2014 at 18:11
  • 11
    My guess is you have a variable in your code named sum that is probably a float. Commented Feb 5, 2014 at 18:11

4 Answers 4

17

It looks like you happen to assign to a variable named sum in the same scope as the call above, thereby hiding the builtin sum function.

Sign up to request clarification or add additional context in comments.

3 Comments

If you need further help in identifying the problematic spot, you should post more of your code for context.
hehe, classic mixup 🤦🏻‍♂️ Thanks for setting us straight @tawmas. Can't tell you how many times I've overridden a builtin. I am surprised that PyCharm does not complain about it!
Oh wow, your comment just saved me some valuable time. I couldn't get my head around this issue. Thanks!
3

This problem happened for me as well. And I did not create any variable with 'sum' name. I solved the problem by changing 'sum' function to 'numpy.sum'.

Comments

0

This saved me too, as pnz showed above. I was racking my brains trying to figure out why 'sum' wasn't working. It wasn't called anywhere else in my script, and resolved by using 'numpy.sum'. Seems like default 'sum' doesn't work well with a list of floats.

This failed: xlist = [1.5, 3.5, 7.8] print(sum(xlist))

This worked: xlist = [1.5, 3.5, 7.8] print(numpy.sum(xlist))

Comments

0

#import builtins

numbers = [1,2,3,4,5,1,4,5]

total = builtins.sum(numbers)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.