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.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to