[Python-bugs-list] [ python-Bugs-510384 ] Separate Scope for List Comprehensions
noreply@sourceforge.net
noreply@sourceforge.net
Wed, 30 Jan 2002 11:34:46 -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 11:34
Message:
Logged In: NO
Sorry, the bug that was fixed was about global scope.
This was discussed when list comprehensions were designed,
and it was a conscious decision that won't be changed.
It's the same as for regular for loops.
--Guido (still can't log in)
----------------------------------------------------------------------
Comment By: Nobody/Anonymous (nobody)
Date: 2002-01-30 11:33
Message:
Logged In: NO
Oops, I misread your complaint. The bug that was fixed was
that sometimes the variables end up in the *global* scope.
This was discussed when it was designed and it was decided
to do it this way. It's similar to what a regular for loop
does:
for i in range(10): pass
print i
prints 10.
I'll reject this when I can log in to SF again.
--Guido (again)
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2002-01-30 08:20
Message:
Logged In: YES
user_id=80475
I verified that this has NOT been fixed in Python 2.2:
Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license" for more
information.
IDLE 0.8 -- press F1 for help
>>> i=10
>>> temp = [str(i) for i in range(5)]
>>> print i
4
----------------------------------------------------------------------
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