11

If you add an integer to a list, you get an error raised by the __add__ function of the list (I suppose):

>>> [1,2,3] + 3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "int") to list

If you add a list to a NumPy array, I assume that the __add__ function of the NumPy array converts the list to a NumPy array and adds the lists

>>> np.array([3]) + [1,2,3]
array([4, 5, 6])

But what happens in the following?

>>> [1,2,3] + np.array([3])
array([4, 5, 6])

How does the list know how to handle addition with NumPy arrays?

5
  • 7
    "How does the list know how to handle addition with numpy arrays." - it doesn't, [1,2,3].__add__(np.array([3])) will fail, but Python reverses the arguments if the first attempt fails and np.array([3]).__radd__([1,2,3]) (__add__ is called if __radd__ isn't defined) works. Commented Oct 15, 2015 at 14:32
  • 1
    @jonrsharpe Shouldn't rather __radd__ be called? Commented Oct 15, 2015 at 14:34
  • 1
    @Christoph if it's defined, yes; clarified Commented Oct 15, 2015 at 14:34
  • @jonrsharpe: I don't think a + b will ever call b.__add__. Python doesn't assume that addition is commutative. You may be thinking of the augmented assignment operators, where a += b will first try a.__iadd__, but then fall back to __add__ (and further to b.__radd__) if necessary. Commented Oct 16, 2015 at 4:07
  • @Blckknght you're absolutely right, I guess that's what I was thinking of. Commented Oct 16, 2015 at 7:19

2 Answers 2

12

list does not know how to handle addition with NumPy arrays. Even in [1,2,3] + np.array([3]), it's NumPy arrays that handle the addition.

As documented in the data model:

  • For objects x and y, first x.__op__(y) is tried. If this is not implemented or returns NotImplemented, y.__rop__(x) is tried. If this is also not implemented or returns NotImplemented, a TypeError exception is raised. But see the following exception:

  • Exception to the previous item: if the left operand is an instance of a built-in type or a new-style class, and the right operand is an instance of a proper subclass of that type or class and overrides the base’s __rop__() method, the right operand’s __rop__() method is tried before the left operand’s __op__() method.

When you do

[1,2,3] + np.array([3])

what is internally called is

np.array([3]).__radd__([1,2,3])
Sign up to request clarification or add additional context in comments.

Comments

8

It is because of the __radd__ method of np.array, check out this link : http://www.rafekettler.com/magicmethods.html#numeric (paragraph Reflected arithmetic operators).

In facts, when you try [1,2,3].__add__(np.array([3])), it raises an error, so np.array([3]).__radd__([1,2,3]) is called.

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.