globally available objects

S

Steve

Hi list,
I speak with ignorance but I'd like to know if something like
this is even possible:
I would like to create an object while importing a module only the
first time it get's imported. I wonder imports already work in this
manner. For example, in a module (say global_objs.py) I would have:

import mylogger
import error_hdlr

my_log_obj = mylogger.init_log() # This may involve some file I/O
my_err_obj = myerror_hdlr.init() # Thiis also might involve some file I/O

now, at run time, would mylogger.init_log() be called each for each
import of global_objs.py ?? I feel it would. Assuming this is correct,
how do I ensure that once created, the objects in global_objs.py do
not get recreated. ....maybe I need to redefine __import__ or
__getattr__ ?? If yes, how do I know from within such functions
whether the objects have been created ??

ehe ...am I making sense to anyone else here ??

Regards
Steve
 
D

Diez B. Roggisch

Steve said:
ehe ...am I making sense to anyone else here ??

You do, but if you put the same amount of time writing this message in
writing a simple test module for yourself and importing it, you've had seen
that it in fact works the way you want it: a module is only imported once.

No offense intended, btw - I just wanted to point out that in python its
common to simply fire up the interpreter and poke around to test things.
 
S

Steve

Hi Diez,

You do, but if you put the same amount of time writing this message in
writing a simple test module for yourself and importing it, you've had seen
that it in fact works the way you want it: a module is only imported once.
Yes I did know that, however, I was not sure if objects that are
created during imports would be the same. Anyways, I did create a test
scriptlets and verified that it indeed is that way !!!!
------------------------------------------------------------------------------------
[steve@sfernandez20 ~]$ cat foo.py
print "calling file().read()"
s = file('test').read()

[steve@sfernandez20 ~]$ cat first.py
#!/usr/bin/python
from foo import s
print id(s)

[steve@sfernandez20 ~]$ cat second.py
#!/usr/bin/python
from foo import s
print id(s)

[steve@sfernandez20 ~]$ python

Python 2.3.3 (#2, Feb 17 2004, 11:45:40)
[GCC 3.3.2 (Mandrake Linux 10.0 3.3.2-6mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more information.calling file().read()
1077624320
1077624320
------------------------------------------------------------------------------------

No offense intended, btw - I just wanted to point out that in python its
common to simply fire up the interpreter and poke around to test things.

No offense taken, thanks for the suggestion. I did try some tests but
with the wrong intent. I guess I just got bogged down with the actual
way to emulate the behaviour (tried overloading __import__ and stuff
like that). Guess I need more coffee !!

In any case, I still would like to have a look at Pierre singleton
implementation,

Regards
Steve
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top