Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This depends entirely on the object <code>i</code>. </p> <p><code>+=</code> calls the <a href="http://docs.python.org/2/reference/datamodel.html#object.__iadd__" rel="noreferrer"><code>__iadd__</code> method</a> (if it exists -- falling back on <code>__add__</code> if it doesn't exist) whereas <code>+</code> calls the <a href="http://docs.python.org/2/reference/datamodel.html#object.__add__" rel="noreferrer"><code>__add__</code> method</a><sup>1</sup> or the <a href="http://docs.python.org/2/reference/datamodel.html#object.__radd__" rel="noreferrer"><code>__radd__</code> method in a few cases</a><sup>2</sup>. </p> <p>From an API perspective, <code>__iadd__</code> is supposed to be used for modifying mutable objects <em>in place</em> (returning the object which was mutated) whereas <code>__add__</code> should return a <em>new instance</em> of something. For <em>immutable</em> objects, both methods return a new instance, but <code>__iadd__</code> will put the new instance in the current namespace with the same name that the old instance had. This is why </p> <pre><code>i = 1 i += 1 </code></pre> <p>seems to increment <code>i</code>. In reality, you get a new integer and assign it "on top of" <code>i</code> -- losing one reference to the old integer. In this case, <code>i += 1</code> is exactly the same as <code>i = i + 1</code>. But, with most mutable objects, it's a different story:</p> <p>As a concrete example:</p> <pre><code>a = [1, 2, 3] b = a b += [1, 2, 3] print a #[1, 2, 3, 1, 2, 3] print b #[1, 2, 3, 1, 2, 3] </code></pre> <p>compared to:</p> <pre><code>a = [1, 2, 3] b = a b = b + [1, 2, 3] print a #[1, 2, 3] print b #[1, 2, 3, 1, 2, 3] </code></pre> <p>notice how in the first example, since <code>b</code> and <code>a</code> reference the same object, when I use <code>+=</code> on <code>b</code>, it actually changes <code>b</code> (and <code>a</code> sees that change too -- After all, it's referencing the same list). In the second case however, when I do <code>b = b + [1, 2, 3]</code>, this takes the list that <code>b</code> is referencing and concatenates it with a new list <code>[1, 2, 3]</code>. It then stores the concatenated list in the current namespace as <code>b</code> -- With no regard for what <code>b</code> was the line before.</p> <hr> <p><sup><sup>1</sup>In the expression <code>x + y</code>, if <code>x.__add__</code> isn't implemented or if <code>x.__add__(y)</code> returns <code>NotImplemented</code> <strong>and</strong> <em><code>x</code> and <code>y</code> have different types</em>, then <code>x + y</code> tries to call <a href="http://docs.python.org/2/reference/datamodel.html#object.__radd__" rel="noreferrer"><code>y.__radd__(x)</code></a>. So, in the case where you have </sup></p> <p><sup><code>foo_instance += bar_instance</code></sup></p> <p><sup>if <code>Foo</code> doesn't implement <code>__add__</code> or <code>__iadd__</code> then the result here is the same as </sup></p> <p><sup><code>foo_instance = bar_instance.__radd__(bar_instance, foo_instance)</code></sup></p> <p><sup><sup>2</sup>In the expression <code>foo_instance + bar_instance</code>, <code>bar_instance.__radd__</code> will be tried before <code>foo_instance.__add__</code> <em>if</em> the type of <code>bar_instance</code> is a subclass of the type of <code>foo_instance</code> (e.g. <code>issubclass(Bar, Foo)</code>). The rational for this is because <code>Bar</code> is in some sense a "higher-level" object than <code>Foo</code> so <code>Bar</code> should get the option of overriding <code>Foo</code>'s behavior.</sup></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload