[Python-ideas] [Python-Dev] hello, new dict addition for new eve ?

Bruce Leban bruce at leapyear.org
Fri Dec 30 20:37:21 CET 2011

On Fri, Dec 30, 2011 at 9:12 AM, Eric Snow <ericsnowcurrently at gmail.com>wrote:

> On Fri, Dec 30, 2011 at 10:02 AM, Guido van Rossum <guido at python.org>
> wrote:
> > What I meant is similar to set union on the keys, where if a key exists
> in
> > both dicts, the value in the result is equal to one of the values in the
> > operands (and if the value is the same for both operands, that value is
> also
> > the result value).
> +1
> This is the one I was thinking of too.
> -eric

My first thought on reading Julien's message was that {1:2} + {1:3} =>
{1:5} would be useful when doing reduction,  There already is an operation
that combines two dictionaries and does not combine values: update. Guido's
version of + is a shorthand for:

def __add__(self, other):
  result = self.copy()
  return result.update(other)

Is saving one line of code be enough of a benefit?

I think dicts are complex enough that if new functionality is added, it
would be better to use named methods which can have meaningful names and
are easier look up in the documentation. Furthermore, methods are more
flexible as this example shows:

def merge(self, other, merger=lambda x, y: x + y):
  """Merges another dictionary into this one, combining values."""
  for k in other:
    if k in self:
      self[k] = merger(self[k], other[k])
      self[k] = other[k]

--- Bruce
Follow me: http://www.twitter.com/Vroo http://www.vroospeak.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20111230/c3a243b8/attachment.html>

More information about the Python-ideas mailing list