Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Python supports multiple assignments on the same line:</p> <pre><code>&gt;&gt;&gt; a = b = c = 2 &gt;&gt;&gt; a 2 &gt;&gt;&gt; b 2 &gt;&gt;&gt; c 2 </code></pre> <p>Since you are assigning a first to a list(a mutable object in python) and then assigning both part of that list(which is mutable) and a to the contents of a third item, python is ok with it. If you had replaced your middle value with something that wasn't mutable, you would get an error.</p> <pre><code>&gt;&gt;&gt; a = 'foo' = 'bar' File "&lt;input&gt;", line 1 SyntaxError: can't assign to literal &gt;&gt;&gt; a = {}['foo'] = 'bar' &gt;&gt;&gt; a 'bar' </code></pre> <p>Interestingly, a line like this still throws an error, because you are attempting to assign the values 3 and 4 to the literal values 1 and 2.</p> <pre><code>&gt;&gt;&gt; a = [1, 2] = [3, 4] File "&lt;input&gt;", line 1 SyntaxError: can't assign to literal </code></pre> <p>While this does not:</p> <pre><code>&gt;&gt;&gt; a = [1, 2][:] = [3, 4] &gt;&gt;&gt; a [3, 4] </code></pre> <p>The key in this case is that by using slice notation you are assigning to the range inside of the list, instead of reassigning to the literal values in the list.</p> <p>Additionally, the lines</p> <pre><code>&gt;&gt;&gt; a = [[], []] = [{}, {}] &gt;&gt;&gt; a [{}, {}] </code></pre> <p>and</p> <pre><code>&gt;&gt;&gt; a = [b, c] = 3, 4 &gt;&gt;&gt; b 3 &gt;&gt;&gt; c 4 </code></pre> <p>Are valid, the first one because the inner lists and dicts are both mutable and iterable, and the second one because you are assigning 3, and 4 to the variables b and c instead of literals(thanks Sven :D).</p> <p><strong>Edit</strong> To answer the op's final question.</p> <pre><code>&gt;&gt;&gt; a = 1 &gt;&gt;&gt; b = 2 &gt;&gt;&gt; a,b = b,a = a,b &gt;&gt;&gt; a 2 &gt;&gt;&gt; b 1 </code></pre> <p>This just comes down to the final(right-most) assignment of b, a = a, b. Observe:</p> <pre><code>&gt;&gt;&gt; a, b = 'a', 'b' &gt;&gt;&gt; a, b = b, a = a, b = b, a &gt;&gt;&gt; a 'b' &gt;&gt;&gt; b 'a' &gt;&gt;&gt; a, b = b, a = b, a &gt;&gt;&gt; a 'a' &gt;&gt;&gt; b 'b' </code></pre> <p>It's confusing, but the way I interpret this is that a and b are not re-evaluated / assigned new values until the statement is finished, so the assignment that matters is the one farthest to the right. If the last statement is a, b = a, b or b, a = b, a, a and b's values don't change. If it is a, b = b, a or b, a = a, b, the values are switched. </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