Are for-loop name list expressions legal?
<p>In CPython 2.7.10 and 3.4.3, and PyPy 2.6.0 (Python 2.7.9), it is apparently legal to use expressions (or some subset of them) for the name list in a for-loop. Here's a typical for-loop:</p>

<pre><code>&gt;&gt;&gt; for a in [1]: pass
...
&gt;&gt;&gt; a
1
</code></pre>

<p>But you can also use attributes from objects:</p>

<pre><code>&gt;&gt;&gt; class Obj(object): pass
...
&gt;&gt;&gt; obj = Obj()
&gt;&gt;&gt; for obj.b in [1]: pass
...
&gt;&gt;&gt; obj.b
1
</code></pre>

<p>And you can even use attributes from expressions:</p>

<pre><code>&gt;&gt;&gt; for Obj().c in [1]: pass
...
</code></pre>

<p>But not all expressions appear to work:</p>

<pre><code>&gt;&gt;&gt; for (True and obj.d) in [1]: pass
...
  File "&lt;stdin&gt;", line 1
SyntaxError: can't assign to operator
</code></pre>

<p>But they do so long as the attribute is on the outside?</p>

<pre><code>&gt;&gt;&gt; for (True and obj).e in [1]: pass
...
&gt;&gt;&gt; obj.e
1
</code></pre>

<p>Or something is assignable?</p>

<pre><code>&gt;&gt;&gt; for {}['f'] in [1]: pass
...
</code></pre>

<p>I'm surprised any of these are legal syntax in Python. I expected only names to be allowed.  Are these even supposed to work? Is this an oversight? Is this an implementation detail of CPython that PyPy happens to also implement?</p>

<p>As <a href="https://docs.python.org/3/reference/compound_stmts.html#the-for-statement" rel="nofollow">documented</a>, the "variable" in the <code>for</code> syntax can be any <code>target_list</code>, which, as <a href="https://docs.python.org/3/reference/simple_stmts.html#grammar-token-target_list" rel="nofollow">also documented</a> means anything that can be on the left-hand side of an assignment statement.  You obviously can't use something like <code>(True and obj.d)</code>, because there's no way to assign a value to that.</p>

python language-lawyer