Deprecating reload() ???

David MacQuigg dmq at gain.com
Fri Mar 12 13:43:25 CET 2004


On Fri, 12 Mar 2004 16:27:36 +1100, user at domain.invalid wrote:

>Ellinghaus, Lance wrote:
>
>> I agree that it does not really work as most people think it does, but how
>> would you perform the same task as reload() without the reload()? 
>> 
>> Would this be done by "del sys.modules['modulename']" and then perform an
>> 'import'?
>
>Ah, interesting! I've been doing this:
>
>del modulename; import modulename
>
>but it doesn't pick up any recent changes to the 
>modulename.py file.
>
>That's a better solution than exiting Python and 
>starting it up again. Thanks!

This doesn't work.  It doesn't matter if you first "del modulename".
All that does is remove the reference to "modulename" from the current
namespace.

   testimport.py
a = 8
print 'a =', a

>>> import testimport
a = 8
>>> ## ==> change a to 9 in testimport.py
>>> testimport.a
8
>>> dir()
['__builtins__', '__doc__', '__name__', 'testimport']
>>> del testimport
>>> dir()
['__builtins__', '__doc__', '__name__']
>>> import testimport
>>> dir()
['__builtins__', '__doc__', '__name__', 'testimport']
>>> testimport.a
8  <== The old module is still in memory !!!
>>> a = testimport.a
>>> dir()
['__builtins__', '__doc__', '__name__', 'a', 'testimport']
>>> reload(testimport)
a = 9
>>> testimport.a
9  <== reload() changes new fully-qualified references
>>> a
8  <== but not any other references that were set up before.
>>> 

I know this is the *intended* behavior of reload(), but it has always
seemed to me like a bug.  Why would you *ever* want to keep pieces of
an old module when that module is reloaded?

Seems to me we should *fix* reload, not deprecate it.

-- Dave




More information about the Python-list mailing list