loop scope

Arthur ajsiegel at optonline.com
Mon Mar 15 15:07:45 EST 2004


On Mon, 15 Mar 2004 10:26:41 -0800, Donn Cave <donn at u.washington.edu>
wrote:
>
>Taking list comprehensions, because that's what the original
>post took at least as an example and because that's the only
>case that is new enough to half seriously consider changing -
>
>   t = f(a/c)
>   ... # and then later,
>   sl = [t * t for t in range(3)]
>
>In this case, I would argue that it's an error for t to escape
>the expression scope _because_ it's used outside that scope -
>actually I think that was where we came in. 

Yeah, I guess that is where the danger is, if I had been previously
bound "t" and had it rebound against my expectations Which was not
exactly the case I fell into.  Thinking about it, I - and hopefully
others - at least have an intuitive sense of knowing what they don't
know. 

I guess there were always enough variable names at my disposal that I
have avoided using something as a iterator variable that I have used
for some other purpose in the same scope. Just to be safe. And knowing
I don't fully understand what is happening under the covers.. Good
boy. me.

Art
> My personal view
>is that a list comprehension should have its own internal scope,
>period, but is that what `intuition' would lead most Python
>programmers to expect?  I don't know.
>
>   Donn Cave, donn at u.washington.edu




More information about the Python-list mailing list