Why variable used in list comprehension available outside?
Peng Yu
pengyu.ut at gmail.com
Wed May 2 20:52:25 EDT 2012
Hi,
The following example demonstrates the variable 'v' used in the list
comprehension is accessible out site the list comprehension.
I think that 'v' should be strictly local. Does anybody know where
this behavior is documented and why it is designed this way?
~/linux/test/python/man/library/__buildin__/class/{/iteritems$ cat main1.py
#!/usr/bin/env python
d = {'one': 10, 'two': 20}
for k, v in d.iteritems():
print k, v
x=[2*v for v in [1, 2, 3]]
print x
print k, v
~/linux/test/python/man/library/__buildin__/class/{/iteritems$ ./main1.py
two 20
[2, 4, 6]
two 3
one 10
[2, 4, 6]
one 3
--
Regards,
Peng
More information about the Python-list
mailing list