471,073 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

globally available objects

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
Jul 18 '05 #1
3 1038
Steve wrote:

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.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
Hi Diez,

On Fri, 29 Oct 2004 12:58:19 +0200, Diez B. Roggisch <de*********@web.de> wrote:
Steve wrote:

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.

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.

import first, second 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
Jul 18 '05 #3
> In any case, I still would like to have a look at Pierre singleton
implementation,


There are some recipes on aspn's cookbook - go google :)
--
Regards,

Diez B. Roggisch
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Thomas Gagne | last post: by
21 posts views Thread by steve | last post: by
13 posts views Thread by Jean-François Doyon | last post: by
1 post views Thread by Matt Kruse | last post: by
2 posts views Thread by robin.bruce | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.