On Jun 18, 11:28 pm, greg <[email protected]> wrote: > nn wrote: > > This is certainly an odd one. This code works fine under 2.6 but fails > > in Python 3.1. > > >>>>class x: > > > ... lst=[2] > > ... gen=[lst.index(e) for e in lst] > > In 3.x it was decided that the loop variables in a list > comprehension should be local, and not leak into the > surrounding scope. This was implemented by making the > list comprehension into a nested function. > > Unfortunately this leads to the same unintuitive > behaviour as a genexp when used in a class scope. > -- I don't get this - the only local loop variable is "e", not lst. And lst is used twice in the generator expression, which is being complained about?
It would generally be the case that a nested function would have access to its enclosing scope. In any case, even if/when the current behavior is explained or rationalized, it certainly appears un-intuitive, and that is another level of "error"! :-) -- http://mail.python.org/mailman/listinfo/python-list
