In NumPy, how do I set array b's data to reference the data of array a?
<p>Say I have ndarray a and b of compatible type and shape. I now wish for the data of b to be referring to the data of a. That is, without changing the array b object itself or creating a new one. (Imagine that b is actually an object of a class derived from ndarray and I wish to set its data reference after construction.) In the following example, how do I perform the b.set_data_reference?</p>

<pre class="lang-python prettyprint-override"><code>import numpy as np
a = np.array([1,2,3])
b = np.empty_like(a)
b.set_data_reference(a)
</code></pre>

<p>This would result in b[0] == 1, and setting operations in one array would affect the other array. E.g. if we set a[1] = 22 then we can inspect that b[1] == 22.</p>

<p>N.B.: In case I controlled the creation of array b, I am aware that I could have created it like </p>

<pre><code>b = np.array(a, copy=True)
</code></pre>

<p>This is, however, not the case.</p>

<p>Usually when functions are not always supposed to create their own buffer they implement an interface like</p>

<pre><code>def func(a, b, c, out=None):
    if out is None:
        out = numpy.array(x, y)
    # ...
    return out
</code></pre>

<p>that way the caller can control if an existing buffer is used or not.</p>

python python-3.x numpy