loop scope

David MacQuigg dmq at gain.com
Mon Mar 15 18:38:20 CET 2004


On Mon, 15 Mar 2004 14:43:20 GMT, Arthur <ajsiegel at optonline.com>
wrote:

>>
>>I think I may have misunderstood your original question.  The title of
>>the thread is 'loop scope', but your question seems to be specifically
>>on the loop iteration variable 't'.  Just think of it as a normal
>>variable in the current local scope.
>
>I can. But it's not my first inclination.  And I would speculate I am
>far from alone. Having subtleties to be aware of in something as
>fundamenatal as a for loop is not a great thing, IMO. On the hand
>hand, it was a long time before any subtleties here had any practical
>implications to me.  I had been getting away with ignorance.  But its
>not a good feeling to know that.
>
>t=None #(or something) 
>
>required prior to a loop would  assure I am conscious of what I am
>getting myself into.  Without it, it seems it isn't safe to assume the
>user understands the full implications of simply complying with
>required loop syntax.
>
>Wouldn't something like this make sense:
>
>With a loop iteration variable declared explicitly in the curent scope
>and prior to the loop, it survives the loop.  Otherwise it is treated
>as a placeholder within the loop, and goes out of scope at its
>conclusion.

This is a little too tricky for my taste.  Often we need to 'break'
from a loop, and subsequently use the value of 't'.  If we forget to
"declare" 't' outside the loop, then we will have a situation where
you get a run-time error if the loop ends without a break.

What is the benefit of keeping 't' out of the surrounding local
scope?"  This whole thread seems pointless unless there is a
substantial benefit to changing the current behavior.

-- Dave




More information about the Python-list mailing list