[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 16:42:57 CEST 2011


>>    # Possible behavior
>>    x = {'key1': 1}
>>    x.replace(key1=3) == {'key1': 3}
>>    x == {'key1': 1} # Original variable is unchanged
>>
> 2011/5/5 Giuseppe Ottaviano <giuott at gmail.com>:
> class using(object):
>    def __init__(self, obj):
>        self._wrappee = obj
>
>    def unwrap(self):
>        return self._wrappee
>
>    def __getattr__(self, attr):
>        def wrapper(*args, **kwargs):
>            getattr(self._wrappee, attr)(*args, **kwargs)
>            return self
>        return wrapper

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()



More information about the Python-ideas mailing list