5 May 2011 16:37
Re: [Python-Dev] What if replacing items in a dictionary returns the new dictionary?
>> 2011/4/29 Roy Hyunjin Han <starsareblueandfaraway@...>: >> 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 <giuott@...>: > 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(Continue reading)
RSS Feed