<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">
<div><div>On Apr 4, 2010, at 3:17 , Stephen Hansen wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Where exactly does this common sense come from? A list comprehension is </div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">basically syntactic sugar over a for loop, and...</div></blockquote><div><br></div><div>well, since I've been bitten by this particular wart, I was surprised to see that the list comp didn't have it's own scope.  If it's syntactic sugar for a for-loop, I figured that rather than converting </div><div><br></div><div><div>d = dict()</div><div>for r in [1,2,3]:</div><div>    d[r] = [r for r in [4,5,6]]</div><div><br></div><div>to </div><div><br></div><div><div>d = dict()</div><div>for r in [1,2,3]:</div><div>    </div><div>    L=[]</div><div>    for r in [4,5,6]:</div><div>        L.append(r)</div><div>    d[r] = L</div><div><br></div><div>it would convert it to something like:</div><div><br></div><div><div>d = dict()</div><div>for r in [1,2,3]:</div><div>    </div><div>    L=[]</div><div>    for _r in [4,5,6]:</div><div>        L.append(_r)</div><div>    d[r] = L</div><div><br></div><div><br></div><div>still a for-loop, but without the surprising side-effect.  I'm glad they fixed this one!</div><div><br></div><div>surely, once you know, it's easy to overcome.  as a curiosity, I just went and skimmed the section: </div><div><br></div><div><a href="http://docs.python.org/tutorial/datastructures.html">http://docs.python.org/tutorial/datastructures.html</a></div><div><br></div><div>which describes list comps, and didn't see any mention of this behavior.  it's probably there, but it certainly doesn't jump out.</div></div></div></div><div><br></div><div><br></div><span class="Apple-tab-span" style="white-space: pre; ">                  </span>bb</div><div><br></div><div><span class="Apple-style-span" style="font-size: 13px; "><div>-- </div><div>Brian Blais</div><div><a href="mailto:bblais@bryant.edu">bblais@bryant.edu</a></div><div><a href="http://web.bryant.edu/~bblais">http://web.bryant.edu/~bblais</a></div><div><a href="http://bblais.blogspot.com">http://bblais.blogspot.com</a>/</div><br class="Apple-interchange-newline"></span><br class="Apple-interchange-newline"></div></body></html>