Message53455
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
|
|
Date |
User |
Action |
Args |
2007-08-23 16:02:00 | admin | link | issue510384 messages |
2007-08-23 16:02:00 | admin | create | |
|