Re: reload from ( was Python FAQ ... )

Guido.van.Rossum@cwi.nl
Thu, 18 Nov 1993 17:50:34 +0100

> Howabout using the usual trivial solution: when import sucks a file from
> disk, have it stash away a copy of the dev maj/min, inum, and ctime. Then on
> a subsequent request to import the same module, it could start by ignoring
> the cache, repeat the search algorithm until it gets to the file, then check
> the stat info for that file to see if it can trust the cache. This would
> take care of the general case: not only if you modify the file, but if you
> remove it and replace it with a symlink pointing elsewhere, or even remove
> it from a directory eariler on your search path, exposing something later on
> your search path.

Oh noooooh!

First, in any large program a particular module will be imported
dozens of times, so it would mean hundreds or thousands of extra stat
operations. There is also a lot of code around that imports a module
inside a function, possibly executing the import statement hundreds or
thousands of times during execution of one program. This all assumes
that import is virtually free after the first time -- which in fact it
is: just one dictionary lookup in sys.modules.

Second, it would mean that if you happen to be editing a module while
a program using the module is executing, different parts of the
program may be using two different incompatible versions of the
module.

Third, there are many modules that go through a long initialization
sequence when they are imported for the first time. The semantics of
this would entirely change if there were the possibility that an
imported module is initialized more than once. (Using reload() on
such modules would not be a good idea -- that's why you have to be a
little bit careful with reload().)

In fact, I resist the notion that sys.modules is a cache. It is the
collection of modules that have already been initialized. (Note that
this quite elegantly deternines the initialization order of imported
modules, which in C++ is done using a lot of handwaving...)

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>