RE: Use of global

Hammond, Mark (MHammond@jm.cmutual.com.au)
Sat, 01 Apr 95 11:30:00 PST

>This is a newbie question, but anyway:
No worries!

>Why does this generate a NameError?
>
>
>>>> global gvar
>>>> def f():
>... gvar = 'xxx'
>...

The global is in the wrong place. The global is a cue to indicate the named
variable is to be put into the global name space - ie, the module (or main)

This is what you are after:
>>> def f():
... global gvar
... gvar = 'xxx'
...
>>> f()
>>> gvar
'xxx'
>>>

Mark.