import headaches/gripes

Paul Prescod paulp at ActiveState.com
Sat Mar 10 12:06:45 EST 2001


Costas Menico wrote:
> 
>...
> 
> Why can't the import statement be smart enough to check if a file was
> changed to import it anyway? All it has to do is compare .py to the
> last import date/time.

I think there are two reasons:

 1. Even checking file dates has a performance cost. It involves going
to the hard disk after all. Code like this isn't rare in Python

def foo():
   import bar
   ....

 2. What would happen if you built a big, long-running application and
then changed a file while it was running. Would you really want one
import statement in the file to load the new version? Then you would
have objects floating around from the old version alongside objects from
the new version. When it crashed due to the inconsistency, it would be
very confusing.

Pythonwin is one big long-running Python app. In that particular
situation you might prefer it to act differently but Python doesn't
really have a feature that allows what you want...and even if it did,
you'd have to be careful because you could get into the same situation
where you had old objects hanging around with the old behavior. That
happens even with a reload today.

> if module_exists(utils):
>         reload(utils)
> else:
>         import utils

You can make it simpler than that:

import utils
reload(utils)

The first operation will continue silently and quickly if the module is
loaded. Then it will be reloaded. The only "cost" is that the very first
time you would load the module twice in a row...

-- 
Python:
    Programming the way
    Guido
    indented it.




More information about the Python-list mailing list