loop scope

Arthur ajsiegel at optonline.com
Thu Mar 11 22:08:09 CET 2004


>>>a=[1,2,3]
>>> for p in a:
	print p
1
2
3
>>> p
3

My naive expectation was that p would be 'not defined' from outside
the loop.

I know this is not news. 

In fact I had come across some discussion of this fact, but apparently
didn't register it.

As I got myself surprised by it - specifically  in the context of list
comps, where I think it is particularly surprising:

>>> b=[4,5,6]
>>> [t*2 for t in b]
[8, 10, 12]
>>> t
6

Is this anywhere useful, or more of an artifact?

Art
 



More information about the Python-list mailing list