How to have application-wide global objects

Jeremy Sanders jeremy+complangpython at jeremysanders.net
Fri Jul 14 05:14:04 EDT 2006


Fredrik Lundh wrote:
 
> if you got some other result, you didn't just import the same thing
> twice...

I think you may be incorrect, or I have misinterpreted you.

Try this:

** In test.py ********************
import sys

import foo.bar

print foo.bar.myvar
foo.bar.myvar = 42
print foo.bar.myvar

sys.path.insert(0, 'foo')
import bar

print bar.myvar

** In foo/__init__.py ************
# this is blank

** In foo/bar.py *****************
myvar = 10


If you run test.py, then you get the output
10
42
10

When I would have expected 10, 42, 42. The bar module gets imported twice,
once as foo.bar and secondly as bar. The value of 42 in myvar does not get
retained, as there are two copies of the module imported.

-- 
Jeremy Sanders
http://www.jeremysanders.net/



More information about the Python-list mailing list