globally available objects

From: Steve (lonetwin_at_gmail.com)
Date: 10/29/04


Date: Fri, 29 Oct 2004 16:04:13 +0530
To: Python List <python-list@python.org>

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



Relevant Pages

  • Re: My python interpreter became mad !
    ... fashion that it is invoked when one does "import re"? ... This is a classic case of a script when imported instead of being executed) being given the same name as a Python-included module and being executed in the current directory and hence ends up importing itself. ... > first time, all works right. ...
    (comp.lang.python)
  • Re: WMI - invalid syntax error?
    ... The problem only seems to occur when importing the file and using it ... It works the first time, but not the second, third, etc. ... computer and had the same issue...but I am wondering if I start anew if ... Prev by Date: ...
    (comp.lang.python)
  • Re: globally available objects
    ... > I would like to create an object while importing a module only the ... > first time it get's imported. ... From the viewpoint of Python code, ... the name indicated -- either its default 'fetch' name or the 'as' name. ...
    (comp.lang.python)
  • Re: Module question
    ... (f you have your modules importing each other in a circular fashion, then this can cause trouble as x may not be defined yet, so best to avoid that case.) ... (This is done only once -- the first time a module is imported.) ... Bind values in the importing code to the module or its attributes. ... Your init code will be executed once -- in phase 1, no matter how many times an import causes phase two to be performed. ...
    (comp.lang.python)
  • Re: Taiwan government - complaints against the UK SVS
    ... > What are we doing importing anything like these birds at all? ... Don?t forget the Chinese own the majority of British seaports and are well ... Beijing may keep quiet, but non-mainland Chinese may be much ... Regards ...
    (uk.business.agriculture)