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

  • Thread starter Philippe C. Martin
  • Start date
P

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__.


Is there a clean way to do this (I'd like to avoid global)?


Regards,

Philippe


--
*********************
Philippe C. Martin
SnakeCard LLC
www.snakecard.com
*********************
 
T

Thomas Guettler

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top