Proposal: local variable declarations and type advice

Paul Rubin phr-n2002a at
Thu Feb 28 01:45:31 EST 2002

Jeff Shannon <jeff at> writes:
> Ah, yes, got it.  That does make sense.  I don't see using try/finally as
> being too onerous, in cases where you *need* to have certain names cleaned up
> (which would seem a moderately rare occurence, to me), but I can see that
> being able to declare a local scope would be a bit cleaner.  I'm inclinded to
> think that the need for local-scope-inside-function-scope is not frequent
> enough to justify a language change to be cleaner than try/finally, but maybe
> it's just because I've been working in relatively straightforward application
> areas.

The idea of using a local scope is actually to avoid shadowing an
outer scope.  Of course if you KNEW you were shadowing an outer scope
in a way that could cause a bug (you're not always sure--say you want
to use "i" as a loop index) then you'd just choose a different
variable name.  So the possible relative rareness of the occurence
doesn't matter.  What matters is having to study that much more of
someone else's grotty code and possibly make a mistake, every time
you want to add a patch.  Of course it would be better to understand
everything perfectly and for the surrounding code to not be grotty,
but programming in the real world doesn't work like that.

The designers of C originally had the same idea as Python, that you
needed global and local variables but inner local scopes weren't
needed.  Eventually they saw the need to introduce local scopes in C,
so you can now put declarations introducing new variables after any
open curly brace.  C++ went further and you can put declarations
almost anywhere.

More information about the Python-list mailing list