import keyword behaviour - performance impact if used multiple times?

Nick Coghlan ncoghlan at iinet.net.au
Fri Jan 14 22:44:47 EST 2005


neophyte wrote:
> Nick Coghlan wrote:
> 
>> > Is
>> > this something to do with system modules being singletons?
>>
>>They aren't singletons in the GoF design pattern sense. However,
> 
> Python's import
> 
>>machinery operates in such a way that it takes effort to get multiple
> 
> version of
> 
>>the same module into memory at the same time (it *can* be done, but
> 
> you have to
> 
>>work at it).
> 
> Given that this is exactly what I want, how can you do it?
> 

If you just want to reload an existing module, use the builtin "reload" function.

Getting multiple versions of a module into sys.modules at the same time isn't 
something I've ever actually wanted to do, but the following will do it:

Py> import sys
Py> sys.modules["sys1"] = sys
Py> del sys.modules["sys"]
Py> import sys
Py> import sys1
Py> sys is sys1
False

However:
1. Doing this at all is probably a bad idea (since you may end up duplicating 
objects that are meant to be unique within the process, and any C-extension code 
will still be shared between the multiple versions of the module)
2. Doing it to 'sys' like I just did is an even worse idea, since you 
*definitely* end up doing 1 :)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list