Re: [Python-ideas] [Python-Dev] What if replacing items in a dictionary returns the new dictionary?

1 view
Skip to first unread message

Roy Hyunjin Han

unread,
May 5, 2011, 10:37:04 AM5/5/11
to Giuseppe Ottaviano, python...@python.org, pytho...@python.org
>> 2011/4/29 Roy Hyunjin Han <starsareblu...@gmail.com>:
>> It would be convenient if replacing items in a dictionary returns the
>> new dictionary, in a manner analogous to str.replace(). What do you
>> think?
>>
>> # Current behavior
>> x = {'key1': 1}
>> x.update(key1=3) == None
>> x == {'key1': 3} # Original variable has changed
>>
>> # Possible behavior
>> x = {'key1': 1}
>> x.replace(key1=3) == {'key1': 3}
>> x == {'key1': 1} # Original variable is unchanged
>>
> 2011/5/5 Giuseppe Ottaviano <giu...@gmail.com>:
> In general nothing stops you to use a proxy object that returns itself
> after each method call, something like
>
> 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
>
>
> d = dict()
> print using(d).update(dict(a=1)).update(dict(b=2)).unwrap()
> # prints {'a': 1, 'b': 2}
> l = list()
> print using(l).append(1).append(2).unwrap()
> # prints [1, 2]

Cool! I never thought of that. That's a great snippet.

I'll forward this to the python-ideas list. I don't think the
python-dev people want this discussion to continue on their mailing
list.
_______________________________________________
Python-ideas mailing list
Python...@python.org
http://mail.python.org/mailman/listinfo/python-ideas

Roy Hyunjin Han

unread,
May 5, 2011, 10:42:57 AM5/5/11
to Giuseppe Ottaviano, python...@python.org
>>    # Possible behavior
>>    x = {'key1': 1}
>>    x.replace(key1=3) == {'key1': 3}
>>    x == {'key1': 1} # Original variable is unchanged
>>
> 2011/5/5 Giuseppe Ottaviano <giu...@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()

Roy Hyunjin Han

unread,
May 5, 2011, 11:19:16 AM5/5/11
to Giuseppe Ottaviano, python...@python.org
2011/5/5 Giuseppe Ottaviano <giu...@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())

Reply all
Reply to author
Forward
0 new messages