[Tutor] Is Python really dynamic ?

Benoit Dupire bdupire@seatech.fau.edu
Sat, 24 Mar 2001 10:30:34 -0500


My goal is to dynamically import  a module called 'func'  from 'prog1.py'
and to get the references to the objects that 'func' created.

# prog1.py

class foo:
    def __init__(self, module):
        self.aa=__import__(module)              # dynamically import func
        chRef= getattr(self.aa, 'setheading')         # get reference to
func objects
        chRef.run()

my_foo = foo('func')

# ----------------------------------
# func.py
class SetHeading:
    def run(self):
        print 'test'

setheading=SetHeading()

It works fine. The result is:
>>>reload(prog1)
test
<module 'prog1' from 'C:\Python20\prog1.pyc'>

Now I change func.py, replacing 'setheading=SetHeading()' with
'toto=SetHeading()'
Normally, this should create an error when I run prog1.py, because the
setheading object does not exist anymore and prog1 references it.

However:
>>> reload(prog1)
test
<module 'prog1' from 'C:\Python20\prog1.pyc'>


prog1.py is not recompiled. func is not reimported!

How can I force prog1.py to dynamically reload  module='func' ?

Any ideas ?
Thank you very much
Benoit