What if replacing items in a dictionary returns the new dictionary?
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
Hi! Seems like a question for python-ideas mailing list, not for python-dev. On Fri, Apr 29, 2011 at 10:27:46AM -0400, Roy Hyunjin Han wrote:
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
You can implement this in your own subclass of dict, no? Oleg. -- Oleg Broytman http://phdru.name/ phd@phdru.name Programmers don't die, they just GOSUB without RETURN.
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. class ReplaceableDict(dict): def replace(self, **kw): 'Works for replacing string-based keys' return dict(self.items() + kw.items())
On Fri, Apr 29, 2011 at 4:05 PM, Roy Hyunjin Han
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]
2011/4/29 Roy Hyunjin Han
: 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
: 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.
Roy Hyunjin Han wrote:
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 _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/marks%40dcs.gla.ac.uk
Could you please post this to python-ideas, rather than python-dev Python-dev is about aspects of the implementation, not significant language changes. Mark.
On Fri, 29 Apr 2011 10:27:46 -0400, Roy Hyunjin Han
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?
This belongs on python-ideas, but the short answer is no. The general language design principle (as I understand it) is that mutable object do not return themselves upon mutation, while immutable objects do return the new object. -- R. David Murray http://www.bitdance.com
2011/4/29 R. David Murray
2011/4/29 Roy Hyunjin Han
: It would be convenient if replacing items in a dictionary returns the new dictionary, in a manner analogous to str.replace()
This belongs on python-ideas, but the short answer is no. The general language design principle (as I understand it) is that mutable object do not return themselves upon mutation, while immutable objects do return the new object.
Thanks for the responses. Sorry for the mispost, I'll post things like this on python-ideas from now on. RHH
participants (5)
-
Giuseppe Ottaviano
-
Mark Shannon
-
Oleg Broytman
-
R. David Murray
-
Roy Hyunjin Han