Newbie edit/compile/run cycle question

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Mon Dec 10 12:13:52 EST 2007


MartinRinehart at gmail.com a écrit :
> Bruno,
> 
> Please explain why the NOP import is a GoodThing. Use small words
> please. I'm not as young as I used to be.

Each module that need access to another module must explicitely import 
it. This means that, in a typical program, your main script will import 
a couple modules, each importing other modules, etc. Chances are that 
some modules - and not necessarily light-weight ones - may end up being 
imported dozens or more times.

Now the import statement does 2 things : first load the module, then 
inject it in the current namespace. Obviously, the costly operation is 
the first one, and obviously it doesn't need to be done more than once 
for a given process. I'd even say it should *not* be done more than once 
for a given process, since reloading a module doesn't affect objects 
using the already existing code.

So caching the module on first import is obviously the right thing to do.

> I didn't know about reload(), but now that I'm informed on that point
> I'm still using
> 
> os.remove('foo.pyc')
> reload(foo)

Mmm... I stopped using the reload function many years ago - in practice, 
  it's the perfect way to go on wild goose chase for bugs that don't 
even exist - but IIRC, you shouldn't have to manually remove the .pyc file.

> A single command to do that would be nice.

That was the purpose of the reload function AFAICT. Now it has proven to 
be such a disaster in practical use that everyone and her sister found 
better ways - like the ones that have already been suggested here.




More information about the Python-list mailing list