How to have application-wide global objects
Fredrik Lundh
fredrik at pythonware.com
Fri Jul 14 05:23:32 EDT 2006
Jeremy Sanders 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.
you've misinterpreted what Python means by "a module".
> Try this:
> import foo.bar
here you import the module named "foo.bar"
> print foo.bar.myvar
> foo.bar.myvar = 42
> print foo.bar.myvar
>
> sys.path.insert(0, 'foo')
> import bar
here you import the module named "bar".
thanks to your path munging, that happens to point to the same file, but
Python's import system doesn't give a damn about that. it identifies
modules by their *names*, not their file system location.
> 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.
no, the "bar.py" *file* gets loaded twice, first as the "foo.bar"
module, and then as the "bar" module.
</F>
More information about the Python-list
mailing list