Suggestion: Python global scope

Nick Dumas drakonik at gmail.com
Tue Jul 15 09:13:02 EDT 2008


-----BEGIN PGP SIGNED MESSAGE-----
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.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkh8ol4ACgkQLMI5fndAv9ieGwCgi51Vs45tgj3mbom0BeM/nBzL
XwsAnjRZr9An617xXyiWp9AVBv3FQ3+z
=EFee
-----END PGP SIGNATURE-----



More information about the Python-list mailing list