[Python-Dev] Visibility scope for "for/while/if" statements
Gareth McCaughan
gmccaughan at synaptics-uk.com
Thu Sep 22 19:55:13 CEST 2005
Alexander Myodov wrote:
> Thus, your example falls to case 1: "i" variable is newly declared for
> this loop. Well, we don't reuse old value of i to start the iteration
> from a particular place, like below?
>
> i = 5
> for i in [3,4,5,6,7]:
> print i,
>
> More general, the variables could be assumed local only to the *same
> or higher* indentation level.
So (since you're talking about "if" as well as "for" and "while")
you're suggesting that
x = 0
if foo():
x = 1
else:
x = 2
should always leave x == 0? Or that the same bit of code, without
the first line, should always leave x undefined?
> JC> Python semantics seem to have been following the rule of "we are all
> JC> adults here".
>
> I always believed that the programming language (as any computer
> program) should slave to the human, rather than a human should slave
> to the program.
Right. And some of us humans *don't want* the change you're
proposing.
For what it's worth, I think it might well have been
better if "for" and comprehensions had made their loop
variables block-local; especially comprehensions. But
making every for/while/if introduce a new level of scoping
would be horrible. Perhaps you think it's what you want,
but I think if you tried it for a month then you'd change
your mind.
--
g
More information about the Python-Dev
mailing list