On 2/24/06, James Y Knight <[EMAIL PROTECTED]> wrote: > On Feb 24, 2006, at 1:54 AM, Greg Ewing wrote: > > Thomas Wouters wrote: > >> On Thu, Feb 23, 2006 at 05:25:30PM +1300, Greg Ewing wrote: > >> > >>> As an aside, is there any chance that this could be > >>> changed in 3.0? I.e. have the for-loop create a new > >>> binding for the loop variable on each iteration. > >> > >> You can't do that without introducing a whole new scope > >> for the body of the 'for' loop, > > > > There's no need for that. The new scope need only > > include the loop variable -- everything else could > > still refer to the function's main scope. > > No, that would be insane. You get the exact same problem, now even > more confusing: > > l=[] > for x in range(10): > y = x > l.append(lambda: (x, y)) > > print l[0]() > > With your suggestion, that would print (0, 9). > > Unless python grows a distinction between creating a binding and > assigning to one as most other languages have, this problem is here > to stay.
The more practical complaint is that list comprehensions use the same namespace as the block that contains them. It's much easier to miss an assignment to, say, i in a list comprehension than it is in a separate statement in the body of a for loop. Since list comps are expressions, the only variable at issue is the index variable. It would be simple to fix by renaming, but I suspect we're stuck with the current behavior for backwards compatibility reasons. Jeremy _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com