Odd behavior with imp.reload and logging

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Sep 22 10:25:39 CEST 2011


On Wed, 21 Sep 2011 23:47:55 -0500, Andrew Berg wrote:

> On 2011.09.21 11:22 PM, Steven D'Aprano wrote:
>> You could
>> try something like this (untested):
> That works. Thanks!
> This makes me wonder what else stays around after a reload 

Practically everything. A reload doesn't delete anything, except as a 
side-effect of running the module again.

Don't think of reloading as:

  * Revert anything the module is responsible for.
  * Delete the module object from the import cache.
  * Import the module in a fresh environment.

Instead, think of it as:

  * Re-import the module in the current environment.


In practice, you won't often see such side-effects, because most modules 
don't store state outside of themselves. If they store state *inside* 
themselves, then they will (almost always) overwrite that state. E.g. 
this will work as expected:


state = [something]


But this leaves state hanging around in other modules and will be 
surprising:

import another_module
another_module.state.append(something)


My guess is that the logging module uses a cache to save the logger, 
hence there is state inadvertently stored outside your module.


Another place where reload() doesn't work as expected:

>>> import module
>>> a = module.MyClass()
>>> reload(module)
<module 'module' from 'module.pyc'>
>>> b = module.MyClass()
>>> type(a) is type(b)
False


Objects left lying around from before the reload will keep references 
open to the way things were before the reload. This often leads to 
confusion when modules are edited, then reloaded. (Been there, done that.)



-- 
Steven



More information about the Python-list mailing list