Suggestion: Python global scope

Anonymous Bastard bastard at example.org
Tue Jul 15 13:57:41 CEST 2008


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.



More information about the Python-list mailing list