pythonic way to optimize access to imported value?
Michael Hudson
mwh at python.net
Tue Nov 19 06:47:57 EST 2002
bokr at oz.net (Bengt Richter) writes:
> 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.
I don't think this is so hard:
def load_file(file, modname):
import new
mod = new.module(modname)
exec open(file).read() in mod.__dict__
return mod
Then mod should only have the one reference:
>>> import new
>>> sys.getrefcount(new.module('temp'))
1
Also see the imp module; some of the hooks there don't insert in
sys.modules (but I can't remember which ones now).
Cheers,
M.
--
Two decades later, well-known hacker Henry Spencer described the
Perl scripting language as a "Swiss-Army chainsaw", intending to
convey his evaluation of the language as exceedingly powerful but
ugly and noisy and prone to belch noxious fumes. -- the jargon file
More information about the Python-list
mailing list