[Python-Dev] Explicit Lexical Scoping (pre-PEP?)
Michael Chermside
mcherm at mcherm.com
Wed Jul 5 19:18:21 CEST 2006
Guido writes:
[discussion of how to fix the can't-bind-outer-scope-vars wart]
> I think we have to continue to search for a solution that extends the
> idea of global declarations.
>
> I've proposed extending its meaning to refer to the nearest outer
> scope where the variable is set; if there is no such scope it's an
> error. This will break a small number of program but probably not very
> many; still, it'll require a future statement or waiting until Python
> 3.0. The downside is that "global" is not a very intuitive word for
> this new meaning.
I disagree with your last statement -- I think "global" _is_ a very
intuitive word for this. As I understand it, in programming "global"
has two meanings, closely intertwined. One is "universal, same
throughout the system". For instance, "The singleton pattern is used
to create a single, global instance of a type." The second meaning is
the term "global variable". This term developed (I believe) in
languages that had only two scopes: local-to-current-function and
global-to-entire-program. But the term "global variable" refers to
any variable whose assignment is a "side effect", regardless of
whether that variable is global-to-entire-program, global-to-module,
or even global-to-enclosing-function. I have even heard the term
"global variable" (mis)used to refer to any kind of side effect.
Anyhow, in Python only builtins is _really_ global -- even today's
global keyword only refers to module scope. So I believe that it
would be a very reasonable interpretation of "global" to mean
"not local", and implement as "search enclosing scopes in order
to find the binding".
-- Michael Chermside
More information about the Python-Dev
mailing list