user2763361
user2763361

Reputation: 3919

TypeError: 'float' object not callable

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

sum([-450.0,950.0])

Upvotes: 0

Views: 9383

Answers (4)

Mahi
Mahi

Reputation: 492

#import builtins

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

total = builtins.sum(numbers)

Upvotes: 0

Kishan Parekh
Kishan Parekh

Reputation: 1

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

Upvotes: 0

pnz1337
pnz1337

Reputation: 246

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

Upvotes: 3

tawmas
tawmas

Reputation: 7823

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.

Upvotes: 17

Related Questions