Suggestion: Python global scope

John Roth johnroth1 at gmail.com
Tue Jul 15 09:15:35 EDT 2008


On Jul 15, 5:57 am, Anonymous Bastard <bast... at example.org> wrote:
> I've been tossing this idea in my mind for some time now:
>
> In Python, declaring a variable using the global statement automatically
> makes it available in all subsequent scopes.
>
> But to me, it makes more sense to use the global statement to 'import' a
> variable from the global scope into the current scope. For instance:
>
> [code]
> global X
> X = 1
>
> def P():
>      X = 2
>      print X
>      global X
>      print X
>
> print X
> P()
> print X
> [code]
>
> Currently, this will print 1, 2, 2 and 2. But if global would be limited
> to current scope, it would print 1, 2, 1, 1.
>
> 'X = 2' would work on the local version of X, 'global X' will 'import'
> the global X into the local scope, so any actions on X would reference
> the global X, rather than previous X.

I suppose it could be done, but I for one would be against it on
several grounds.

First, global operates somewhat as a declaration currently.
Declarations are easier for most developers to understand than
redefinitions of a variable on the fly.

Second, following up the first point, it redefines a variable. While
this is common practice, it's unfortunate since it leads to some
amount of confusion. It also seems like it would be most useful in
large, poorly structured methods, which is exactly the wrong way to
go.

Finally, as Gerhard Haring has said, it's backward incompatible, so
the window for considering it has past: the 3.x series is pretty much
frozen.

John Roth



More information about the Python-list mailing list