While familiarizing myself with numpy
, I noticed an interesting behaviour in numpy
arrays:
import numpy as np
arr = np.array([1, 2, 3])
scale = lambda x: x * 3
scale(arr) # Gives array([3, 6, 9])
Contrast this with normal Python lists:
arr = [1, 2, 3]
scale = lambda x: x * 3
scale(arr) # Gives [1, 2, 3, 1, 2, 3, 1, 2, 3]
I'm curious as to how this is possible. Does a numpy
array override the multiplication operator or something?