469,281 Members | 2,484 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,281 developers. It's quick & easy.

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

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
*********************
Jul 18 '05 #1
1 1101
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/
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Hans-Joachim Widmaier | last post: by
6 posts views Thread by Pete Davis | last post: by
4 posts views Thread by blackhawk | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.