|  | What is module initialization? |  | |
| | | Guest |  |
| Posted: Tue Sep 02, 2008 11:32 am Post subject: What is module initialization? |  |
| |  | |
Hi,
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.
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.
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). This happen only at the start of the program and at rest of the places in the program that global variable is not accessed.
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? And how can I ensure that module initialization is done only once?
Thanks and regards, Rajat |
| |
| | | Marc 'BlackJack' Rintsch |  |
| Posted: Tue Sep 02, 2008 11:46 am Post subject: Re: What is module initialization? |  |
| |  | |
On Tue, 02 Sep 2008 14:32:30 +0100, dudeja.rajat wrote:
| Quote: | 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.
| Quote: | 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!
| Quote: | 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 |
| |
| | | Bruno Desthuilliers |  |
| Posted: Tue Sep 02, 2008 12:04 pm Post subject: Re: What is module initialization? |  |
| |  | |
dudeja.rajat@gmail.com a écrit :
| Quote: | 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).
| Quote: | 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".
| Quote: | 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.
| Quote: | 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 ?"
| Quote: | 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.
| Quote: | 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. |
| |
| | | Guest |  |
| Posted: Tue Sep 02, 2008 12:32 pm Post subject: Re: What is module initialization? |  |
| Quote: | 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 ?"
|
But if I do :- #question.py from myglobals import * myglobals.answer = "WTF ?"
will this work? |
| |
| | | Wojtek Walczak |  |
| Posted: Tue Sep 02, 2008 2:46 pm Post subject: Re: What is module initialization? |  |
On Tue, 2 Sep 2008 15:32:07 +0100, dudeja.rajat@gmail.com wrote:
| Quote: | But if I do :- #question.py from myglobals import * myglobals.answer = "WTF ?"
will this work?
|
Why won't you try? In this case you should receive NameError.
-- Regards, Wojtek Walczak, LINK |
| |
| | | Fredrik Lundh |  |
| Posted: Tue Sep 02, 2008 3:22 pm Post subject: Re: What is module initialization? |  |
dudeja.rajat@gmail.com wrote:
| Quote: | # myglobals.py: answer = 42
# question.py import myglobals myglobals.answer = "WTF ?"
But if I do :- #question.py from myglobals import * myglobals.answer = "WTF ?"
will this work?
|
with the above definition of myglobals, no. "from myglobals import" doesn't add the module object to the importing module's namespace.
have you read:
LINK
?
</F> |
| |
| | | Guest |  |
| Posted: Tue Sep 02, 2008 5:21 pm Post subject: Re: What is module initialization? |  |
On Tue, Sep 2, 2008 at 6:22 PM, Fredrik Lundh <fredrik@pythonware.com> wrote:
| Quote: | dudeja.rajat@gmail.com wrote:
# myglobals.py: answer = 42
# question.py import myglobals myglobals.answer = "WTF ?"
But if I do :- #question.py from myglobals import * myglobals.answer = "WTF ?"
will this work?
with the above definition of myglobals, no. "from myglobals import" doesn't add the module object to the importing module's namespace.
have you read:
LINK
?
/F
-- LINK
|
Thanks for your help guys that I got my problem resolved.
Regards, Rajat |
| |
|
|