Peter said:
Tim Jarman wrote:
Is there a simple way to replace the contents of a dictionary entirely
with those of another.
for lists we can do
L1[:] = L2
How about:
d1 = { "spam" : "eggs" }
d2 = { "gumby" : "my brain hurts!"}
d1 is d2 False
d1 = dict(d2)
This rebinds the name d1 to a copy of d2 and will not affect other
references to the original d1.
Peter
Peter has the essence of the problem. Attempts to change sys.modules
have strange effects eg try this simple script
import sys
omods = sys.modules
sys.modules = omods.copy()
print 'start', len(sys.modules), len(omods)
import urlparse
print 'after import', len(sys.modules), len(omods)
In my executions it's len(omods) that changes
so we need a way to copy the original sys.modules and then quickly
replace uf we want to restore the original value.
The actual replace part of L1[:]=L2 happens in a single opcode and is
therefore atomic. The same cannot be said of the .clear, .update sequence.