K
Ken Pu
Hi all,
I observed an interesting yet unpleasant variable scope behaviour with
list comprehension in the following code:
print [x for x in range(10)]
print x
It outputs:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
9
So the list comprehension actually creates a variable x which is
somewhat unexpected.
Is there a way for me keep the iterating variable in list
comprehension local to the list comprehension?
Any comments on the current behaviour of Python is greatly appreciated.
Ken
I observed an interesting yet unpleasant variable scope behaviour with
list comprehension in the following code:
print [x for x in range(10)]
print x
It outputs:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
9
So the list comprehension actually creates a variable x which is
somewhat unexpected.
Is there a way for me keep the iterating variable in list
comprehension local to the list comprehension?
Any comments on the current behaviour of Python is greatly appreciated.
Ken