[color=blue][color=green][color=darkred]
>>>a=[1,2,3]
>>> for p in a:[/color][/color][/color]
print p
1
2
3[color=blue][color=green][color=darkred]
>>> p[/color][/color][/color]
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:
[color=blue][color=green][color=darkred]
>>> b=[4,5,6]
>>> [t*2 for t in b][/color][/color][/color]
[8, 10, 12][color=blue][color=green][color=darkred]
>>> t[/color][/color][/color]
6
Is this anywhere useful, or more of an artifact?
Art
>>>a=[1,2,3]
>>> for p in a:[/color][/color][/color]
print p
1
2
3[color=blue][color=green][color=darkred]
>>> p[/color][/color][/color]
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:
[color=blue][color=green][color=darkred]
>>> b=[4,5,6]
>>> [t*2 for t in b][/color][/color][/color]
[8, 10, 12][color=blue][color=green][color=darkred]
>>> t[/color][/color][/color]
6
Is this anywhere useful, or more of an artifact?
Art
Comment