<div dir="ltr"><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif;"><span class="Apple-style-span" style="font-size: small;">I don't think the for loop is broken. If you want scopes other than global and function, then you should add that explicitly, maybe something like:</span></span><div>
<span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif;"><span class="Apple-style-span" style="font-size: small;"><br></span></span></div><div>a = []</div><div><span class="Apple-style-span" style="border-collapse: collapse; "><div class="Ih2E3d" style="color: rgb(80, 0, 80); ">
<span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif;"><span class="Apple-style-span" style="font-size: small;">for i in range(10):<br></span></span></div><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif;"><span class="Apple-style-span" style="font-size: small;"> local k:</span></span></span></div>
<div><span class="Apple-style-span" style="border-collapse: collapse; "><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif;"><span class="Apple-style-span" style="font-size: small;"> k = i**2<br>
a.append(lambda: k)</span></span></span></div><div><span class="Apple-style-span" style="border-collapse: collapse; "><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif;"><span class="Apple-style-span" style="font-size: small;"> # k is not accessible here</span></span></span></div>
<div><span class="Apple-style-span" style="border-collapse: collapse; "><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif;"><span class="Apple-style-span" style="font-size: small;"><br></span></span></span></div>
<div><span class="Apple-style-span" style="border-collapse: collapse; "><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif;"><span class="Apple-style-span" style="font-size: small;">a[3]() => 9</span></span></span></div>
<div><span class="Apple-style-span" style="border-collapse: collapse; "><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif;"><span class="Apple-style-span" style="font-size: small;"><br></span></span></span></div>
<div><span class="Apple-style-span" style="border-collapse: collapse; "><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif;"><span class="Apple-style-span" style="font-size: small;">which is roughly equivalent to:</span></span></span></div>
<div><br></div><div><span class="Apple-style-span" style="border-collapse: collapse; "><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif;"><span class="Apple-style-span" style="font-size: small;">a = []</span></span></span></div>
<div><span class="Apple-style-span" style="border-collapse: collapse; "><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif;"><span class="Apple-style-span" style="font-size: small;">for i in range(10):</span></span></span></div>
<div><span class="Apple-style-span" style="border-collapse: collapse; "><span class="Apple-style-span" style="font-size: small;"> def local_k():</span></span></div><div><span class="Apple-style-span" style="border-collapse: collapse; "><span class="Apple-style-span" style="font-size: small;"> k = i**2</span></span></div>
<div><span class="Apple-style-span" style="border-collapse: collapse; "><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif;"><span class="Apple-style-span" style="font-size: small;"> a.append(lambda: k)</span></span></span></div>
<div><span class="Apple-style-span" style="border-collapse: collapse; "><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif;"><span class="Apple-style-span" style="font-size: small;"> local_k()</span></span></span></div>
<div><span class="Apple-style-span" style="border-collapse: collapse;"><span class="Apple-style-span" style="font-size: small;"><br></span></span></div><div><span class="Apple-style-span" style="border-collapse: collapse;"><span class="Apple-style-span" style="font-size: small;">--- Bruce</span></span></div>
<div><span class="Apple-style-span" style="border-collapse: collapse; font-family: 'courier new'; font-size: 62px;"><br></span></div></div>