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 10:57:44 EST 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:
http://c2.com/cgi/wiki?PythonSingleton
I suggest you try the different methods out and pick the one best suited
to your situation.
HTH,
Andrew
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