module data member?

Peter J. Bismuti peter.j.bismuti at boeing.com
Tue Nov 13 12:39:21 EST 2007


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



More information about the Python-list mailing list