[Tutor] Is Python really dynamic ?

Benoit Dupire bdupire@seatech.fau.edu
Sat, 24 Mar 2001 11:23:02 -0500


Moshe Zadka wrote:

> On Sat, 24 Mar 2001, Benoit Dupire <bdupire@seatech.fau.edu> wrote:
>
> > How can I force prog1.py to dynamically reload  module='func' ?
>
> Use reload()
>

Thank you Moshe....II already tried reload() but it didn't seem to work..

>>> import prog1
test

That works fine...!

Now change func.py, replacing
'setheading=SetHeading()'
with 'toto=SetHeading()'

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

Why does this still work ? setheading does not exist anymore....
I had no clue... until i run this from the command line, where i got the correct
results, and not from IDLE.

Could someone explain me why IDLE is messing everything up ?

Benoit

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

# prog1.py
class foo:
    def __init__(self, module):
        self.aa=__import__(module)
        reload(self.aa)
        chRef= getattr(self.aa, 'stHeading')
        chRef.run()

my_foo = foo('func')