[EMAIL PROTECTED] a écrit :
Hi,

I found on the net that there is something called module
initialization.

The Python C api has a module init function for C-coded modules. There's no need for such a thing in pure Python modules since all the top-level code is executed when the module is loaded (as a main script or the first time the module is imported).

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.

I'm currently messing with a problem where I'm keeping my global
variables ( or symbols) in a module and the other mdoules in the
project acess these global variables.

remember that there's no such thing as a truely global namespace in Python. "global" really means "module level".

However, there is one case when a module updates one such global
variable

While this is technically legal, you should restrain yourself from doing such a thing, unless you *really* know what you're doing and why.

but the variable is not getting updated in the module
containing global symbols ( variables).

I suspect you didn't use a qualified name when importing. You have to do it this way :

# myglobals.py:
answer = 42

# question.py
import myglobals
myglobals.answer = "WTF ?"

So, I thought of using this module initialization where I will
intialize the module only once to update that variable. Ans in the
rest of the program where ever this module is imported I shall be able
to easily access the update value of the variable.

Could some one provide me a sample code of module intialization?

All statements at the top-level of a module are executed when the module is loaded. That's all it takes wrt/ module initialization.

And
how can I ensure that module initialization is done only once?

Unless you're doing weird things with __import__ or the imp module, you shouldn't have to worry. import do two things : locate, load *and cache* the module *if* it isn't already in cache, and bind names into the importing namespace.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to