A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2011-May/111201.html below:

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

[Python-Dev] What if replacing items in a dictionary returns the new dictionary?Giuseppe Ottaviano giuott at gmail.com
Thu May 5 12:14:34 CEST 2011
On Fri, Apr 29, 2011 at 4:05 PM, Roy Hyunjin Han
<starsareblueandfaraway at gmail.com> wrote:
>>   You can implement this in your own subclass of dict, no?
>
> Yes, I just thought it would be convenient to have in the language
> itself, but the responses to my post seem to indicate that [not
> returning the updated object] is an intended language feature for
> mutable types like dict or list.

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]
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4