This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients
Date 2002-01-29.22:13:09
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
The variables in a list comprehension should not be in 
the enclosing scope.

i=10
temp = [str(i) for i in range(5)]
print i

Should print 10 instead of 4.

Implement the above as:

i=10
def _listcomp():
    for i in range(5):
        yield str(i)
temp = list(_listcomp())
print i

Note, I timed the difference between the existing and 
proposals implementations and found only a 4% decrease 
in speed.

In case someone is already relying on the list 
comprehension variables being in the local scope, a 
deprecation warning or from __future__ in warranted.

Also note, this implementation leaves open the 
possibility of creating generator comprehensions so 
that temp=[yield str(i) for i in range(5)] creates the 
same code as above except that the final 'list' 
coercion is eliminated:  temp=_listcomp

History
Date User Action Args
2007-08-23 16:02:00adminlinkissue510384 messages
2007-08-23 16:02:00admincreate