Scope of variable inside list comprehensions?
Terry Reedy
tjreedy at udel.edu
Mon Dec 5 21:23:17 EST 2011
On 12/5/2011 5:36 PM, Roy Smith wrote:
> Well, in my defense, I did ask a pretty narrow question, "Is id
> guaranteed to be in scope in the print statement?".
Yes for 2.x, guaranteed no for 3.x.
If you had simply asked "Is the loop variable of a list comprehension
guaranteed to be in scope after the list comprehension?", without a
distracting example, that is the answer you would have received.
I intend(ed) to inform, not attack, hence no 'defense' needed.
> While I will
> admit that not knowing whether I could alter the exception, or
> whether id masked a builtin or not does complexify answering some
> questions, those are questions I didn't ask :-)
Except that it bears on the question you did ask because it means that
the code will run in 3.x but with different results, whereas a random
name will fail in 3.x with a NameError.
--
Terry Jan Reedy
More information about the Python-list
mailing list