>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.