[Python-ideas] [Python-Dev] What if replacing items in a dictionary returns the new dictionary?
Roy Hyunjin Han
starsareblueandfaraway at gmail.com
Thu May 5 17:19:16 CEST 2011
2011/5/5 Giuseppe Ottaviano <giuott at gmail.com>:
>> The only thing I would add is obj.copy(), to ensure that the original
>> dictionary is unchanged.
>>
>> class using(object):
>> def __init__(self, obj):
>> self._wrappee = obj.copy()
>
> My example was just a proof of concept, there are many other things
> that may need to be taken care of (for example, non-callable
> attributes).
> BTW, the copy should be done outside. If the object is copied, I'd say
> "using" is a poor choice of name for the proxy.
You're right, I would need to do more work to get it to mimic the
underlying object. I think I will stick with Oleg's suggestion to
subclass dict for now; it's great for unit tests. Thanks for the
idea, though.
class ReplaceableDict(dict):
def replace(self, **kwargs):
'Works for replacing string-based keys'
return dict(self.items() + kwargs.items())
More information about the Python-ideas
mailing list