2

I copied the following example from Python 3.6.1 Documentation, Chapter 9.10 into Jupyter Notebook (Python version 3.6.1):

xvec = [10, 20, 30]

yvec = [7, 5, 3]

sum(x*y for x,y in zip(xvec, yvec))         # dot product

While the official documentation says that it would print 260, I got the following error in Notebook:

----> 4 sum(x*y for x,y in zip(xvec, yvec))
TypeError: 'int' object is not callable

This is definitely not just a question on 'int object is not callable', but more on the palpable error in what is considered to be the gospel for Python.

Seshadri R
  • 1,192
  • 14
  • 24
  • 2
    Possible duplicate of ['int' object is not callable when using the sum function on a list](https://stackoverflow.com/questions/2460087/int-object-is-not-callable-when-using-the-sum-function-on-a-list) – Arya McCarthy Jun 04 '17 at 06:32
  • works for me in a script – robert_x44 Jun 04 '17 at 18:38
  • Yes. In fact, if I change the last line to: `value = sum(x*y for x,y in zip(xvec, yvec)) # dot product` `print(value)`it works inside jupyter notebook itself – Seshadri R Jun 05 '17 at 04:24

2 Answers2

3

If by mistake you have overwritten zip() or/and sum(), which leads that your current code will not work. You can restore their default functionalities using del, like this example:

>>> zip = [1]
>>> zip
[1]
>>> del zip
>>> zip
<function zip>

So, you can try :

>>> del zip
>>> del sum
>>> xvec = [10, 20, 30]
>>> yvec = [7, 5, 3]
>>> sum(x*y for x,y in zip(xvec, yvec))

And it will output:

260
Chiheb Nexus
  • 9,104
  • 4
  • 30
  • 43
2

The code should work as long as you didn't overwrite zip or sum:

>>> xvec = [10, 20, 30]
>>> yvec = [7, 5, 3]
>>> sum(x*y for x,y in zip(xvec, yvec))
260

Make sure that you didn't overwrite them:

>>> sum = 0  # <--- overwrite
>>> sum(x*y for x,y in zip(xvec, yvec))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
falsetru
  • 357,413
  • 63
  • 732
  • 636