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

Andrew James drew at gremlinhosting.com
Tue Dec 7 16:57:44 CET 2004

You're looking for the Singleton pattern to ensure that only one 
instance of your class is instantiated at a time. There's a particularly 
useful discussion about this at:


I suggest you try the different methods out and pick the one best suited 
to your situation.


Philippe C. Martin wrote:
> 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__.
> Is there a clean way to do this (I'd like to avoid global)?
> Regards,
> Philippe

More information about the Python-list mailing list