pythonic way to optimize access to imported value?

Bengt Richter bokr at oz.net
Sun Nov 17 23:03:30 EST 2002


On Mon, 18 Nov 2002 15:49:10 +1300, Greg Ewing <see_reply_address at something.invalid> wrote:

>Bengt Richter wrote:
>
>> I assumed that it is possible to release all references to an imported module, so that
>> the garbage collector can eliminate it from memory
>
>
>That won't happen -- the module remains in the global
>list of modules maintained by the interpreter, even if
>all other references to it go away.
>
You clipped my footnote section and reference, which I thought
indicated that I saw that that is the way it ordinarily works:

"""
 >>>
 >>> import sys
 >>> sys.modules.get('math')
 >>> pi = __import__('math').pi
 import math # builtin
 >>> sys.modules.get('math')
 <module 'math' (built-in)>
 >>> del sys.modules['math']
 >>> sys.modules.get('math')
 >>> import math
 import math # previously loaded (math)
               ^^^^^^^^^^^^^^^^^^^^^^^^

Is there no way to do a private throwaway import?
"""

So, on to the final question, which I'd still be interested in an answer to.
BTW, to me, irreversibly tangled loading operations smack of problems that
you "solve" by rebooting windows.

IOW, IMO there ought to be a way to do a temporary global-side-effect-free
import and delete all traces of its having been done -- without restarting
the interpreter or invoking a second interpreter instance.

After all, it's basically just instantiating an instance of a special class,
isn't it? I would think if I dig in the import code there should be a place
where an isolated instance is created, before it's irreversibly (?) hooked into
the system.

Regards,
Bengt Richter



More information about the Python-list mailing list