Difference between add and iadd?
<p>I'm not understanding the purpose of the in-place operators like iadd, imul, etc.</p>

<blockquote>
  <p>Many operations have an âin-placeâ version. The following functions
  provide a more primitive access to in-place operators than the usual
  syntax does; for example, the statement x += y is equivalent to x =
  operator.iadd(x, y). Another way to put it is to say that z =
  operator.iadd(x, y) is equivalent to the compound statement z = x; z
  += y.</p>
</blockquote>

<p>It seems like I can always use either the in-place or regular operator interchangeably. Is one better than the other?</p>

<p>The "in-place" functions for immutable objects cannot be implemented using an <a href="https://en.wikipedia.org/wiki/In-place_algorithm" rel="nofollow">in-place algorithm</a>, while for mutable objects they could be.  The simple truth is that immutable objects don't change.  </p>

<p>Otherwise, usage of "in-place" versus not "in-place" functions has deep ramifications when considering mutable objects.  Consider the following:</p>

<pre><code>&gt;&gt;&gt; A = [1,2,3]
&gt;&gt;&gt; B = A
&gt;&gt;&gt; id(A)
4383125944
&gt;&gt;&gt; id(B)
4383125944
&gt;&gt;&gt; A = A + [1]
&gt;&gt;&gt; id(A)
4383126376
&gt;&gt;&gt; A += [1]
&gt;&gt;&gt; id(A)
4383126376
</code></pre>

<p>Suppose you are writing some code where it is assumed that B is a soft copy of A (a mutable object).  By not using the "in-place" function when modifying A, desired modifications to B can be quietly missed.  What makes matters worse, is that quick visual inspection of the code makes it seem that the code (e.g., A = A + [2]) is implemented correctly (maybe it makes sense mathematically).  If one really wants to just modify an object and not receive a new object, then the "in-place" function is the right way to go.  </p>

<p>Neither is better than the other. Rather there are specific circumstances under which one might be preferred over the other.</p>

python python-2.7 operators