473,387 Members | 1,541 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,387 software developers and data experts.

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 1403
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__=="__main__"):
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
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...
5
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...
4
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...
7
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...
4
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...
10
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...
11
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...
9
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...
1
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...

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.