replace dict contents
Robin Becker
robin at SPAMREMOVEjessikat.fsnet.co.uk
Tue Jul 27 17:10:42 EDT 2004
Peter Otten wrote:
> Tim Jarman wrote:
>
>
>>On 27 Jul 2004, at 08:56, Robin Becker 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.
--
Robin Becker
More information about the Python-list
mailing list