Loading a file only once into an object and being able to access it from other modules

Thomas Guettler guettli at thomas-guettler.de
Tue Dec 7 10:53:41 EST 2004


Am Tue, 07 Dec 2004 09:25:57 -0600 schrieb Philippe C. Martin:

> This is a basic question I'm sure but I do not know wether to use __builtin__, 
> global, or a static method: 
> 
> I have a very large XML file that I load into dictionnaries defined in a class 
> located in a module that is imported in many places.
> 
> Since the loading process is very slow, I would like the file not to be loaded 
> on import or class instantiation, but only once (on first import or class 
> instantiation).
> 
> Currently I have the loading code in the class __init__.

Hi,

You have this module (mymod.py)

print "First import"
xmlfile=....

If you import mymod the second time, you
won't see "First import" again. The code
without indentation gets executed only once.

You can access your file with mymod.xmlfile.

HTH,
 Thomas


-- 
Thomas Güttler, http://www.thomas-guettler.de/





More information about the Python-list mailing list