[Python-ideas] adding dictionaries
Steven D'Aprano
steve at pearwood.info
Mon Jul 28 18:04:50 CEST 2014
On Mon, Jul 28, 2014 at 03:33:06PM +0000, dw+python-ideas at hmmz.org wrote:
> On Tue, Jul 29, 2014 at 12:59:51AM +1000, Steven D'Aprano wrote:
>
> > > One good reason is that people are still convinced "dict(A, **B)"
> > > makes some kind of sense.
> >
> > Explain please. dict(A, **B) makes perfect sense to me, and it works
> > perfectly too. It's a normal constructor call, using the same syntax
> > as any other function or method call. Are you suggesting that it does
> > not make sense?
>
> It worked in Python 2, but Python 3 added code to explicitly prevent the
> kwargs mechanism from being abused by passing non-string keys.
/face-palm
Ah of course! You're right, using dict(A, **B) isn't general enough.
I'm still inclined to prefer allowing update() to accept multiple
arguments:
a.update(b, c, d)
rather than a += b + c + d
which suggests that maybe there ought to be an updated() built-in, Let
the bike-shedding begin: should such a thing be spelled ?
new_dict = a + b + c + d
Pros: + is short to type; subclasses can control the type of new_dict.
Cons: dict addition isn't obvious.
new_dict = updated(a, b, c, d)
Pros: analogous to sort/sorted, reverse/reversed.
Cons: another built-in; isn't very general, only applies to Mappings
new_dict = a.updated(b, c, d)
Pros: only applies to mappings, so it should be a method; subclasses can
control the type of the new dict returned.
Cons: easily confused with dict.update
--
Steven
More information about the Python-ideas
mailing list