python - Error summing values in a dictionary -
i following error when trying sum values dictionary. expect sum (i.e. 15), error thrown instead.
is bug?
ipython qtconsole 3.1.0 python 2.7.10 |continuum analytics, inc.| (default, may 28 2015, 17:04:42)
d = {'1': 1, '2': 2 , '3': 3, '4': 4, '5': 5} >>> sum(d.values()) --------------------------------------------------------------------------- typeerror traceback (most recent call last) <ipython-input-36-4babd535f17a> in <module>() ----> 1 sum(d.values()) typeerror: 'int' object not callable
you have named variable sum in code points int trying call not sum function. add del sum try code again. example of why should not shadow builtin function names.
in [24]: sum = 4 in [25]: sum((1,2)) --------------------------------------------------------------------------- typeerror traceback (most recent call last) <ipython-input-25-a2ca1bd9c959> in <module>() ----> 1 sum((1,2)) typeerror: 'int' object not callable in [26]: del sum in [27]: sum((1,2)) out[27]: 3
Comments
Post a Comment