Problem with Dynamically unloading a module

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Wed Dec 23 21:50:26 EST 2009


On Wed, 23 Dec 2009 15:18:10 -0800, Carl Banks wrote:

>> > # will unload mod1 assuming mod1 was the only reference to that
>> > module.
>>
>> Which is highly unlikely. Any classes or functions from the module will
>> keep the module alive.
> 
> Actually, they won't.  Neither classes nor functions directly reference
> their module; classes know their module only by name, and functions only
> hold references to the module's namespace, not to the module itself.  So
> if any references to functions defined in the module remain, the module
> dict will stick around, but the module itself may be collected.

Hmmm... so it seems. Well spotted, thanks.

It makes sense to do it that way, as it prevents circular references 
(module contains a class which contains the module which...).



-- 
Steven



More information about the Python-list mailing list