In Ruby there is the merge method (returns a new hash) and the merge! method (modifies the hash that the method is called from). These methods are documented as preferring the values on the passed-in in the case of key collision. ActiveSupport (part of the Ruby on Rails ecosystem) provides reverse_merge and reverse_merge!, which prefer the acting hash's values over the values of the hash that is passed in.
-- Brett Wilkins On 17 October 2013 at 11:58:46 AM, Javier Candeira ([email protected]) wrote: Python culture runs counter to monkeypatching standard library objects, but this looks be easy to do via injecting __add__ (and __iadd__ for "d1 += d2") straight into the dict class. In Ruby it's done In fact it looks so obvious that ... /me googles... https://www.google.com.au/search?q=dict+__add__ The first result is a bug report, but it was rejected before it got to PEP stage even: http://bugs.python.org/issue6410. Contains good rationale for the rejection. J On Thu, Oct 17, 2013 at 11:31 AM, Sam Lai <[email protected]> wrote: > It's almost Friday, so I have a question where I'm pretty sure I'm > missing something obvious. > > Given, > > d1 = { 'a' : 'b' } > d2 = { 'c' : 'd' } > > ... why isn't d3 = d1 + d2 implemented to be equivalent to - > > d3 = { } > d3.update(d1) > d3.update(d2) > > It doesn't work for sets either, but it works in this fashion for > lists. Is this because the operation is non-commutative for sets and > dicts and may result in a loss of data when clashing keys are > involved? Isn't that implicit when working with sets and dicts? > > Sam > _______________________________________________ > melbourne-pug mailing list > [email protected] > https://mail.python.org/mailman/listinfo/melbourne-pug _______________________________________________ melbourne-pug mailing list [email protected] https://mail.python.org/mailman/listinfo/melbourne-pug
_______________________________________________ melbourne-pug mailing list [email protected] https://mail.python.org/mailman/listinfo/melbourne-pug
