473,771 Members | 2,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2271
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**********@gm ail.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.answe r = "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.answe r = "WTF ?"
But if I do :-
#question.py
from myglobals import *
myglobals.answe r = "WTF ?"

will this work?
Sep 2 '08 #4
On Tue, 2 Sep 2008 15:32:07 +0100, du**********@gm ail.com wrote:
But if I do :-
#question.py
from myglobals import *
myglobals.answe r = "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**********@gm ail.com wrote:
># myglobals.py:
answer = 42

# question.py
import myglobals
myglobals.answ er = "WTF ?"

But if I do :-
#question.py
from myglobals import *
myglobals.answe r = "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*****@python ware.comwrote:
du**********@gm ail.com wrote:
>># myglobals.py:
answer = 42

# question.py
import myglobals
myglobals.ans wer = "WTF ?"

But if I do :-
#question.py
from myglobals import *
myglobals.answ er = "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
1993
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 my wrapper. This is acually part of the project, libxmlconf on sourceforge. The newest working version isn't there yet, and cvs is lagged by 6 hours or so. So if you think you want to have a try at this I can tgz the source for you. My...
5
2834
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 to people making that comment about C++. Can someone explain what they mean when they say C++ doesn't support modules? -- STH Hatton's Law: "There is only One inviolable Law" KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com...
21
5225
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 table and 11 sub items with data about each column. When a Row in ListView1 is selected the Data in ListVies2 is loaded to show the correct data. Initially the first row in ListView1 is selected in FormX load
2
1626
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 Form2, I would expect to click on the second Form1 button and get intMyValue = 0. Form2 has code to reinitialize it in its close event, but this doesn't seem
6
1426
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
5152
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 written in python? Thanks, Geoffrey
3
1421
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 namespace. Instead I want to put them in a "module global" namespace that will be the same regardless of whether not this module is imported or run as __main__. Thanks
0
1149
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 threads share the same python module ? The threads are being created from an external C library. I just call PyEval_InitThreads on startup, and then acquire the gil from the c callbacks.
6
1307
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. Simon.
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.