473,218 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

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
Sep 2 '08 #1
6 2224
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
Sep 2 '08 #2
du**********@gmail.com 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.
Sep 2 '08 #3
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?
Sep 2 '08 #4
On Tue, 2 Sep 2008 15:32:07 +0100, du**********@gmail.com wrote:
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,
http://tosh.pl/gminick/
Sep 2 '08 #5
du**********@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:

http://effbot.org/zone/import-confusion.htm

?

</F>

Sep 2 '08 #6
On Tue, Sep 2, 2008 at 6:22 PM, Fredrik Lundh <fr*****@pythonware.comwrote:
du**********@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:

http://effbot.org/zone/import-confusion.htm

?

</F>

--
http://mail.python.org/mailman/listinfo/python-list
Thanks for your help guys that I got my problem resolved.

Regards,
Rajat
Sep 2 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: James S | last post by:
Hi, Basically I've been fighting with this code for a few days now and can't seem to work around this problem. Included is the output, the program I use to get this error and the source code for...
5
by: Steven T. Hatton | last post by:
I've seen people here write that C++ doesn't support modules. What does that mean? 'Module' is a very nebulous term in my book. It probably means something quite different to me than what it does...
21
by: StriderBob | last post by:
Situation : FormX is mdi child form containing 2 ListViews ListView1 contains a list of table names and 4 sub items with data about each table. ListView2 contains a list of the columns on each...
2
by: Bert Szoghy | last post by:
Hello, I am missing something about Visual Basic .NET module variables and window close events. In the following code, after opening Form2 by clicking a button on Form1 and then closing...
6
by: Jack | last post by:
I have a set of functions to wrap a library. For example, mylib_init() mylib_func() mylib_exit() or handle = mylib_init() mylib_func(handle)
2
by: beginner | last post by:
Hi Everyone, An extended module (.pyd) written in C have an init function that is called when the module is imported. Does anyone know if there is a way to provide an init function for a module...
3
by: Peter J. Bismuti | last post by:
How do you define a "module data member" (I want to understand out how this works before making converting to a Class)? Right now I'm defining variables in a module that get put into the global...
0
by: pianomaestro | last post by:
I have a cython extension module "mux" where the initialization is being called more than once (the initmux c function). I am wondering if my use of multiple threads causes this. Can't multiple...
6
by: pianomaestro | last post by:
I have an extension module that gets initialized multiple times because I am using threads. How can this module access global state (not per-thread state) ? It needs to create a singleton. ...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.