global variable confusion

Chris Rebert clp2 at rebertia.com
Tue Feb 3 01:47:21 EST 2009


On Mon, Feb 2, 2009 at 10:25 PM, Robert D.M. Smith
<robert.dm.smith at gmail.com> wrote:
> I have a question on global variables and how to use them.  I have 2 files;
> a.py & b.py
>
> # a.py -----
>
> myvar = { 'test' : '123' }
>
> # -------
> # b.py -----
>
> from a import myvar
>
> def test():
>      a.myvar = { 'blah' : '456' }

test() will fail at runtime with a NameError. You imported only
'myvar' from 'a', not 'a' itself (i.e. there is no variable 'a'
defined with 'b', only the variable 'myvar').
Assuming you changed the import to `import a`, then yes, a.myvar (both
within 'a' itself and when accessed from 'b') would have its value
changed by test().

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list