473,623 Members | 3,366 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

module data member?

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

PS Sorry for the near redundant posts. I'm simply trying to refine my question
as I get a better understanding of what I'm confused about.

Thanks

--
Peter Bismuti
Boeing
Nov 13 '07 #1
3 1412
Peter J. Bismuti wrote:
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__.
There is no "real" global namespace, unless you count __builtins__ - which
you shouldn't :)

All your defined top-level (global) module vars are local to that module.

BUT there is of course one tiny probleme here, that arises when using a
module as main script: then the module will be known under two distinct
names, module-name & __main__. The former of course only if someone imports
the module itself.

The common remedy for this is this:
.... # lots of code of module myself

def main():
# do mainy stuff here

if __name__ == "__main__":
import myself
myself.main()

This of course won't prevent __main__ to be existant, nor running possible
initialization code twice. But if the module is supposed to do some work,
it will operate on the one data all the other importing modules see.

You can take this one step further, using an explicit init()-function that
creates global state (remember, it's only module-global).

Diez
Nov 13 '07 #2
This did the trick for the most part, but it still leaves a copy of the
variable A in the "non-module" global namespace (see output below).

I want A declared global to the module (so as not to be local within the
functions of the module) but not visible outside of the module namespace
(like B, see below). How can this be done?

Thanks again

python -i test.py
:>>print test.A
10
:>>print A
0
:>>print B
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'B' is not defined

____test.py___

A = 0

def getA():
global A
return A

def run():
global A
A = 10

if (__name__=="__m ain__"):
import test
test.run()

______

def main():
# do mainy stuff here

if __name__ == "__main__":
import myself
myself.main()

This of course won't prevent __main__ to be existant, nor running possible
initialization code twice. But if the module is supposed to do some work,
it will operate on the one data all the other importing modules see.

You can take this one step further, using an explicit init()-function that
creates global state (remember, it's only module-global).

Diez
--
Peter Bismuti
Boeing Information Technology
Renton, WA
(425) 234-0873 W
(425) 442-7775 C
Nov 13 '07 #3
Peter J. Bismuti schrieb:
This did the trick for the most part, but it still leaves a copy of the
variable A in the "non-module" global namespace (see output below).

I want A declared global to the module (so as not to be local within the
functions of the module) but not visible outside of the module namespace
(like B, see below). How can this be done?
It can't, unless you remove the A = 0 from the module and put it into
some init function.

To quote myself:

"""
This of course won't prevent __main__ to be existant, nor running possible
initialization code twice. But if the module is supposed to do some work,
it will operate on the one data all the other importing modules see.
"""

You could of course delete everything in __main__ as well, but then...
why do you care about the __main__.A living?
Diez
Nov 13 '07 #4

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

Similar topics

8
1526
by: Philippe C. Martin | last post by:
Hi, I'm getting pretty desperate here: The code below crashes on the last line (but works from a shell). The class 'BC' exists and the loop on self.__BC_EXEC_LIST passes fine. It's got to be something really stupid but I've been looking at it too long I guess.
5
1420
by: Larry Bird | last post by:
I've created a AlertDataClass below within the class I have tables and column that I've create. In the AlertDataAccess class I'm trying to insert data into my tables. AlertDataAccess is a Module that is trying to insert data into the tables. Within the AlertDataClass is subroutine that init and creates ColumnNames. In invoke the AddDataColumnNames() sub to create the column headers. Why can't I see the column names in my module that I'm...
4
2721
by: http://www.visual-basic-data-mining.net/forum | last post by:
Hi Does anyone know how to stay connected to the server and at the same time i can pass the string to and from the module to the form..... What I want: I put the connection at the module..... 1)I can pass the string from the form to the module...
7
3131
by: Paul | last post by:
I will be creating multiple VB.NET applications. I want all of the applications to have some specific global user-defined public constants and public variables (or objects). I currently have them in a module. It works and I can easily reuse that module in each of my applications. But then developers are able to change the module within a specific application without changing it in the other applications. I want them to all be the same....
4
1451
by: Patrick Sullivan | last post by:
I have a data input form with a structure that gets loaded with the input data. I have a module calc.vb with lots of calculations to do on the data in the structure. But I can't access the strutcure's data from the calc module, even though both are public. What other declarations can I do to make it accessible? -- Patrick Sullivan, AA-BA, BA-IT
10
5481
by: Bonzol | last post by:
vb.net Hey there, could someone just tell me what the differnce is between classes and modules and when each one would be used compared to the other? Any help would be great Thanx in advance
11
3093
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend a hand? import regsub
9
14552
by: rahatekarabhijeet | last post by:
I downloaded the sorce from CPAN(PerlMagick-6.32.tar.gz). I followed the normal procedure to install perl module on unix platform, i.e. 1) perl Makefile.PL during the execution of this command i am getting error as: Note (probably harmless): No library found for -lgvc Writing Makefile for Image::Magick 2)make
1
1737
by: Thammarat charoenchai. | last post by:
Are visual basic have something like Data Module in Delphi? that i can drop component to data module and can call to use that component any where in project Thanks you very much.
0
8162
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
8662
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...
0
8603
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8463
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
5560
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
4067
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2593
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1468
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.