Reloading on the fly: was: Re: Python Productivity over C++
James Logajan
JamesL at Lugoj.Com
Sat Jun 10 00:12:03 EDT 2000
Boudewijn Rempt wrote:
> Could you post an example of designing the main program to reload
> submodules? I'd like to get that working, too, and coming from
> languages where that's impossible, I've a bit of trouble coming up
> with a solution...
Several ways are possible. Here is one way. In the main program you might
have:
# main.py
# Run-time modify of a module.
import sys
import os
import m1
m1.F(1) # Try first version of m1.py
del sys.modules['m1'] # The magic bit.
print "Please edit m1.py; press return when ready."
sys.stdin.readline()
import m1
m1.F(1) # Try second version of m1.py
And the module to be modified looks like:
# m1.py
def F(score):
print "Banana"
#print "Mango"
Now a typical run with commenting out the first pritn and uncommenting the
second print in m1.py should lead to an output like:
Banana
Please edit m1.py; press return when ready.
Mango
Hope this helps!
P.S. If you don't even know the name of the module ahead of time, you can
get creative with the "exec" statement!
More information about the Python-list
mailing list