Two questions ( Oracle and modules )

Terry Reedy tjreedy at udel.edu
Mon Dec 7 15:07:58 EST 2009


Gabor Urban wrote:
> Hi guys,

> 2. We have tp write a module that will be imported a couple of places.
> Though we must be sure, this module's initialization is performed only
> once. How can we implement this? I do not know what to google for.....

There are two ways to have one file executed twice to create two module 
objects with two different names. 1) run it as the main file and also 
import it; 2) import with sufficiently different import statements.
(I do not know all the ways to 'accomplish' the latter, but I know it 
can be done.)

So, if you do not execute that module as the main script and you use the 
exact same import statement everywhere, so that the resulting module is 
given the exact same name, then it will be created once and subsequent 
imports will return the same object. This is the normal situation. The 
two cases above are typically accidents.

If you have doubts, put print("Hello, module xxx being initialized.") at 
the top of the module.

tjr






More information about the Python-list mailing list