[Python-bugs-list] [ python-Bugs-510384 ] Separate Scope for List Comprehensions

noreply@sourceforge.net noreply@sourceforge.net
Wed, 30 Jan 2002 07:52:18 -0800


Bugs item #510384, was opened at 2002-01-29 14:13
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=510384&group_id=5470

Category: Python Interpreter Core
Group: Feature Request
Status: Open
Resolution: None
Priority: 5
Submitted By: Raymond Hettinger (rhettinger)
Assigned to: Nobody/Anonymous (nobody)
Summary: Separate Scope for List Comprehensions

Initial Comment:
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



----------------------------------------------------------------------

Comment By: Nobody/Anonymous (nobody)
Date: 2002-01-30 07:52

Message:
Logged In: NO 

This is Guido; I can't log in here on the road.

I believe this has been fixed in Python 2.2, and possibly 
in 2.1.2.  Can you check that?

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=510384&group_id=5470