abjer / sds2019

Social Data Science 2019 - a summer school course
https://abjer.github.io/sds2019
46 stars 96 forks source link

sum([1,2,3]) error: TypeError: 'int' object is not callable #6

Open IAmAndreasSK opened 4 years ago

IAmAndreasSK commented 4 years ago

Hi,

I have been playing around with Python in Jupyter Notebook (not necessarily directly relevant for the assignment). I tried to run the sum() function on a list with a start value of 10:

thirdlist=[1,2,3,4,5] x = sum(thirdlist,10) print(x)

I get the following output:

`--------------------------------------------------------------------------- TypeError Traceback (most recent call last)

in () 1 thirdlist=[1,2,3,4,5] ----> 2 x = sum(thirdlist,10) 3 print(x) TypeError: 'int' object is not callable` I can't seem to understand what I'm doing wrong? I've tried Googling without any look. Moreover, when I try the same code in SublimeText and PythonTutor's Visualizer, it seems to give the correct output (25). Thank you, Andreas
mathiasbruun commented 4 years ago

Hi Andreas,

What does the rest of your notebook look like? Did you "overwrite" the sum() function by assigning an integer value to sum somewhere in the script? That might explain why it works fine when run on its own elsewhere.

Kristianuruplarsen commented 4 years ago

As Mathias pointed out, you have most likely overwritten the variable sum somewhere earlier in your code. I.e. your code contains something like

sum = 2+3 
sum([1,2,3]) # Fails

What you are doing is then something like 5([1,2,3]) and since 5 is not callable you get an error.

IAmAndreasSK commented 4 years ago

Hey Kristian and Mathias, thanks! I did 'define' it in several places. I have renamed them so they're all unique and it works smoothly now :)

Thanks!

Kristianuruplarsen commented 4 years ago

Great! I'm reopening the issue to make it easier for others to find.