When I try to define the +=
in an object Class I want to modify a numpy.ndarray
by reference.
I have the following two objects which are numpy.ndarray
, a1
and a2
:
>>> a1
array([array([ 0.04168576, 0.13111852, 0.91896599]),
array([ 0.81658056, 0.50832376, 1.59519731]),
array([ 0.20646088, 0.13335052, 1.19661452])], dtype=object)
>>> a2
array([array([ 0.25765112, 0.54137219, 0.26067181]),
array([ 0.57738128, 0.45649817, 1.6323892 ]),
array([ 0.2328858 , 0.4922151 , 1.00012122])], dtype=object)
In my class I want to implement a self.__iand__
and want some way to write:
a1.append(a2)
to give the equivalent of the following:
>>> np.hstack((a1,a2))
array([array([ 0.04168576, 0.13111852, 0.91896599]),
array([ 0.81658056, 0.50832376, 1.59519731]),
array([ 0.20646088, 0.13335052, 1.19661452]),
array([ 0.25765112, 0.54137219, 0.26067181]),
array([ 0.57738128, 0.45649817, 1.6323892 ]),
array([ 0.2328858 , 0.4922151 , 1.00012122])], dtype=object)
and to have a1
change by reference.
I want to avoid writing
a1 = np.hstack((a1,a2))
because the actual arrays are very large and this is for a Monte Carlo application and I have got to try to keep performance very fast whenever I can.
Currently when I try to implement this I get the following error:
AttributeError: 'numpy.ndarray' object has no attribute 'append'
Other related Questions:
The following Q is useful for explaining how functions can pass by reference, but doesn't address by problem.
Whereas the answer provided by @SvenMarnach mentions this exact issue, but no solution is provided.
Many Thanks,