loop scope

Arthur ajsiegel at optonline.com
Mon Mar 15 20:40:39 CET 2004


On Mon, 15 Mar 2004 10:38:20 -0700, David MacQuigg <dmq at gain.com>
wrote:

>On Mon, 15 Mar 2004 14:43:20 GMT, Arthur <ajsiegel at optonline.com>
>wrote:
>
>>
>>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.  

Mine too, really.  But it was off the cuff, and illustrative.  And as
I said, the current behavior hasn't caused me any actual problem.
There are probably reasons why one can expect ot get away with being
blissfully ignorant here. 

I couldn't/wouldn't do anything with "t", being unaware that it was
there.  And if I rebind "t" to something else, its the same as if I
was orignally binding it. No harm, no foul. 

Essentailly what happened in the session that brought this up for me
was that I goofed, used "t" without binding it to anything (well
actually I had without realizing) and got a result that I couldn't
make sense of, rather than the error message which I deserved (or
would otherwise have deserved).

I haven't convinced myself it is possible to write sensible code *and*
get caught here, even if unaware of the scoping issue.  Though I
haven't convinced myself it isn't, either.
  

>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