Accessing and updating global variables among several modules

Andrew MacIntyre andymac at bullseye.apana.org.au
Sat Jul 12 05:12:18 EDT 2003


[posted & mailed]
On Sat, 11 Jul 2003, Fuming Wang wrote:

> However, when I modify these global variables in one module, the other
> modules would not see the changes.  Any one has any suggestions? ( I
> don't want to use from A import *)

Use a container, and change the container's contents, eg make the global
"variable" a dictionary, and access the different "global variables" via
the dictionary's keys.

What you're seeing is due to Python's variables being references to
values, rather than actual values.  With the container approach, all the
references will still point to the container, even though the contents
change.  In fact, Python itself does pretty much all name lookup through
dictionaries.

--
Andrew I MacIntyre                     "These thoughts are mine alone..."
E-mail: andymac at bullseye.apana.org.au  (pref) | Snail: PO Box 370
        andymac at pcug.org.au             (alt) |        Belconnen  ACT  2616
Web:    http://www.andymac.org/               |        Australia





More information about the Python-list mailing list