ASP and importing modules

Gordon McMillan gmcm at hypernet.com
Wed Mar 8 09:20:54 EST 2000


maxm asks:

[me - in response to someone reloading modules in ASP]
> > Feature. Put the current top level logic under a main(), and
> > use module.main(). You'll save yourself a lot of import time.
> >
> > - Gordon
> 
> Would you care to elaborate this a bit??
> 
> I think you missed a few steps along the way in this explanation.:-)
> Do you mean that he should create a seperate module with the python code in
> it, in which the reloading should take place?
> 
> Maybe you have a short example?

This is a script:
---------------------
print "yadda yadda yadda"
---------------------
The first time you import it, it does it's thing. Subsequent 
imports do nothing because all the code is top-level.

This is a module:
---------------------
def main():
  print "yadda yadda yadda"
---------------------
Importing it does nothing visible; you need to 
import module
module.main()

This has the same effect as:
import script
reload(script)

but is much more efficient. Well, actually, you'd need:

import sys
if not sys.modules.has_key('script'):
  import script
else:
  reload(script)

reload is an interactive convenience. If you're using it 
programmatically, you've designed something wrong.

- Gordon




More information about the Python-list mailing list