What is module initialization?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Tue Sep 2 09:46:42 EDT 2008


On Tue, 02 Sep 2008 14:32:30 +0100, dudeja.rajat wrote:

> I found on the net that there is something called module initialization.
> Unfortunately, there is not much information for this. However, small
> the information I found module initialization can be of use to me in my
> project.

"Module initialization" is what happens when you import a module the 
first time.  In pure Python modules the module level code is executed and 
in extension modules a special initializing function may be called.

> However, there is one case when a module updates one such global
> variable but the variable is not getting updated in the module
> containing global symbols ( variables).

Sounds unlikely if you *really* update the attribute of the module and 
not just rebind a local name that was bound to the object in the "global" 
module before.  Example:

from spam import egg

egg = 42  # This does *not* change `spam.egg` but just the local binding!


> Could some one provide me a sample code of module intialization? And how
> can I ensure that module initialization is done only once?

Module initialization is only done once, there's nothing to ensure.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list