creating variable in root namespace from module

Steve Holden steve at
Fri Mar 10 18:41:32 CET 2006

MakaMaka wrote:
> Hi again,
> Steve-
> I'm trying to use python to model reliability.  Basically, I want to
> read an XML file in that defines a bunch of variables, that are then
> accessible from the python interpreter.  For example:
> import reliability
> reliability.read_xml("c:\\somefile.xml") #<== defines x1, x2, x3
> result = x1+x2+x3
> This tool will be used by people who are not necessarily coders and I
> don't want them to have to deal with passing global namespaces etc.
> Duncan-
> I like your idea; however, I don't want the user to actually have to
> know the structure of the xml file, so I don't think I can do it this
> way.  There might be 3, 4 or 1000 variables that the function needs to
> create.
> Any other ideas?  What clever technical ways have been used? I know I
> can just have the function add the variables to the global namespace of
> the module and then access them like
> reliability.x1
> or in a dict or something, but I think this might confuse some of my
> potential users who aren't familiar with Object Oriented programming
> (think electrical engineers who still code in Fortran).
> Thanks,
> Justin
Well, one way that springs to mind is to put the definitions in a 
dictionary and then to do


to add them all to the global namespace. You'll have to be careful about 
not shadowing built-in names, though, since the interpreter searched the 
global namespace before the built-ins. You'd have to pass the gloabl 
dict into the function, though, because it lives in another module.

There are various ways you can use data from the sys module to gain 
access to the caller's local namespace. I'm not sure you want to go that 
far, though.

Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd       
Love me, love my blog

More information about the Python-list mailing list