By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 1,960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

module data member?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.