In numpy, it's possible to take slice and modify it:
a = np.array([1, 2, 3, 4, 5])
a[2:4] = [7, 8]
# now a is np.array([1, 2, 7, 8, 5])
How is it implemented?
Suppose you have some 2d array class, Array2d
which has some tricky implementation of 2d array, like 2-dimensional trie. And you want the __getitem__
method to return Array2dSlice
object, which can be modified:
a = Array2d()
...
# [1 2 3]
# a = [4 5 6]
# [7 8 9]
a[1:3, 1:3] = [[0, 0], [0, 0]]
# [1 2 3]
# a = [4 0 0]
# [7 0 0]
If there was operator overloading in Python, I would use it in Array2dSlice
class. But there is no operator overloading. Is there a decent replacement for it?