importing/reloading in Pythonwin

Philip Swartzleonard starx at pacbell.net
Sun Feb 10 17:56:38 EST 2002


Spencer Doidge || Sun 10 Feb 2002 12:33:46p:

> Running Python22's Pythonwin.
> I import all from a script Foo, run some routines, and then edit Foo
> in
> the edit window. I check syntax and save, then click the Import/reload
> button. Returning to the interactive window, I run Foo again, but I
> don't see the expected evidence of the changes I made.
> What am I doing wrong?

When you reload a module, all of the objects in that module get 
recreated and the names in there get refreshed. But any references to 
the old objects are still references to the old objects, which 
persumably hang around till they run out of references and are collected 
by the GC. For example:

x = somemod.x
x is somemod.x -> 1
reload(somemod)
x is somemod.x -> 0

The object formerly known as 'somemod.x' is still there, and x still 
points to it, but the reload has caused 'somemod.x' to point to 
something else.

I don't know if there's ever a way to use reload and get a useful effect 
out of it except -maybe- in the shell if you remeber not to use any of 
the old objects you made again... you may as well just restart the 
program... (Maybe if you can bring the program into a embrionic state 
where nothing has been done yet, reload everything, then continue... but 
it seems like a lot more effort than it's worth :)

Here's a longer version of what happens when you use reload, the 
'reloadtest' module contains:

class Nothing_base(object):
    pass
class Do_nothing(Nothing_base):
    pass


Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help

>>> import reloadtest
>>> from reloadtest import Do_nothing
### ### Equivelent to 'Do_nothing = reloadtest.Do_nothing'
>>> Do_nothing is reloadtest.Do_nothing
1
>>> Do_nothing.__base__ is reloadtest.Nothing_base
1
>>> dn = reloadtest.Do_nothing()
>>> dn.__class__ is Do_nothing
1
>>> dn.__class__ is reloadtest.Do_nothing
1
>>> dn.__class__.__base__ is reloadtest.Nothing_base
1
>>> id(Do_nothing)
11082528
>>> id(reloadtest)
11076864
>>> id(reloadtest.Do_nothing)
11082528
>>> id(reloadtest.Nothing_base)
11083312

>>> reload(reloadtest)
<module 'reloadtest' from 'c:\desk\dv\py\lib\reloadtest.pyc'>

>>> Do_nothing is reloadtest.Do_nothing
0
>>> Do_nothing.__base__ is reloadtest.Nothing_base
0
>>> dn.__class__ is Do_nothing
1
>>> dn.__class__ is reloadtest.Do_nothing
0
>>> dn.__class__.__base__ is reloadtest.Nothing_base
0
>>> id(Do_nothing)
11082528
>>> id(reloadtest)
11076864
>>> id(reloadtest.Do_nothing)
11434928
>>> id(reloadtest.Nothing_base)
11510864
>>> 

-- 
Philip Sw "Starweaver" [rasx] :: www.rubydragon.com



More information about the Python-list mailing list