Module loading trickery

Thomas Jollans thomas at jollybox.de
Wed Oct 6 10:39:17 EDT 2010


On Wednesday 06 October 2010, it occurred to Dave Angel to exclaim:
> On 2:59 PM, Thomas Jollans wrote:
> > <snip>
> > % cat a.py
> > foo = 'Meh.'
> > import b
> > 
> > % cat b.py
> > from a import foo
> > 
> > print(foo)
> > 
> > % python a.py
> > Meh.
> > %
> 
> But there are now two modules containing separate items foo, one is
> called __main__, and the other is called a.

Good point. So let's change the example to match the intentions.

% cat a.py
foo = 'Meh.'
import b

% cat b.py
from a import foo

print(foo)

% cat main.py 
import a

% python3 main.py 
Meh.
% 


> 
> The former is the script you ran, and the latter is the module imported
> by b.  Several problems could occur, including if foo were a list, and b
> appended to it, the original script wouldn't see the change.
> 
> DaveA



More information about the Python-list mailing list