Suggestion: Python global scope

Nick Dumas drakonik at
Tue Jul 15 15:13:02 CEST 2008

Hash: SHA1

The function of the global keyword is to 'push' local variables to the
global scope. If you want to 'import' a variable into a local scope,
pass the variable to your function/method as an argument.

Anonymous Bastard 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.
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla -


More information about the Python-list mailing list