How to copy content of a numpy matrix to another?
<p>I have a simple question about basics of <code>python</code> and <code>numpy</code> module. I have a function as following:</p>

<pre><code>def update_x_last(self, x):
    self.x_last = x
</code></pre>

<p>The class attribute x_last and function argument x are both initialized as of type <code>numpy.matrix</code> and of the same shape. (<code>x.shape = x_last.shape = (4,1</code>)</p>

<p>I have noticed that the code above does not copy the content of the argument <code>x</code> to <code>x_last</code>, but it makes the object <code>x_last</code> point to the address of <code>x</code>.</p>

<p>However what I want to do is the following:</p>

<ul>
<li>Don't change the address of <code>self.x_last</code></li>
<li>Copy only the content of <code>x</code> to <code>self.x_last</code></li>
</ul>

<p>What is the best way to do this?</p>

<p><strong>Edit:</strong>
the requirement 'Don't change the address of 'self.x_last' was unimportant for me. The only required behaviour is the second requirement to copy only the content.</p>

<p>If the shapes are the same, then any of these meet both of your requirements:</p>

<pre><code>self.x_last[...] = x
# or self.x_last[()] = x
# or self.x_last[:] = x
</code></pre>

<p>I'd argue that the first one is probably most clear</p>

<hr>

<p>Let's take a look at your requirements quickly:</p>

<blockquote>
  <p>Copy only the content of x to self.x_last</p>
</blockquote>

<p>Seems reasonable. This means if that if <code>x</code> continues to change, then <code>x_last</code> won't change with it</p>

<blockquote>
  <p>Don't change the address of <code>self.x_last</code></p>
</blockquote>

<p>This doesn't buy you anything. IMO, this is actively worse, because functions using <code>x_last</code> in another thread will see it change underneath them unexpectedly, and worse still, could work with the data when it is incompletely copied from <code>x</code></p>

python numpy matrix