[melbourne-pug] Why can't two dicts be added together?
Brett Wilkins
brett at brett.geek.nz
Thu Oct 17 03:06:39 CEST 2013
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 (javier at candeira.com) 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 <samuel.lai at gmail.com> 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
> melbourne-pug at python.org
> https://mail.python.org/mailman/listinfo/melbourne-pug
_______________________________________________
melbourne-pug mailing list
melbourne-pug at python.org
https://mail.python.org/mailman/listinfo/melbourne-pug
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/melbourne-pug/attachments/20131017/c50d8d79/attachment.html>
More information about the melbourne-pug
mailing list