global declaration from within functions

Rob Andrews rob at jam.rr.com
Wed Aug 15 13:42:20 EDT 2001


> > You can use 'global' to define a new variable.
> >
> But the global keyword did not create the variable. The assignment to the
> variable created it. You can use the 'global' keyword and still not have
> the variable created, you can also create a global variable without using
> the keyword (e.g. by setting a value in the result of globals()).
> 
> The global keyword simply changes how the name is used within a local
> scope. It does not, of itself, create a global variable. A global variable
> is created by assigning to it either directly from outside a function/class
> body; assigning to it from inside a function before(!) or after the use of
> a matching 'global' statement; or assigning to it indirectly such as
> through globals().
> 

I think I've got a bare grasp of the metaphysics involved in global
value assignment in Python. I was really asking more of a pragmatic
question along the lines of why one would really want to change the
value assigned to a global.

I imagine it might be handy for a variety of different hacks one might
use for convenience, but I wonder doing it is actually good practice.

Thanks,
Rob
-- 
A {} is a terrible thing to waste.
Useless Python!
http://www.lowerstandard.com/python



More information about the Python-list mailing list