[Python-ideas] PEP: Dict addition and subtraction

Guido van Rossum guido at python.org
Tue Mar 5 11:45:31 EST 2019


On Tue, Mar 5, 2019 at 2:38 AM Inada Naoki <songofacandy at gmail.com> wrote:

> On Tue, Mar 5, 2019 at 7:26 PM Steven D'Aprano <steve at pearwood.info>
> wrote:
> >
> > On Sat, Mar 02, 2019 at 01:47:37AM +0900, INADA Naoki wrote:
> > > > If the keys are not strings, it currently works in CPython, but it
> may not work with other implementations, or future versions of CPython[2].
> > >
> > > I don't think so.  https://bugs.python.org/issue35105 and
> > > https://mail.python.org/pipermail/python-dev/2018-October/155435.html
> > > are about kwargs.  I think non string keys are allowed for {**d1,
> > > **d2} by language.
> >
> > Is this documented somewhere?
>
> It is not explicitly documented.  But unlike keyword argument,
> dict display supported non-string keys from very old.
>
> I believe {3: 4} is supported by Python language, not CPython
> implementation behavior.
>
>
> https://docs.python.org/3/reference/expressions.html#grammar-token-dict-display
>

I'd like to remove all doubt: {**d1} needs to work regardless of the key
type, as long as it's hashable  (d1 could be some mapping implemented
without hashing, e.g. using a balanced tree, so that it could support
unhashable keys).

If there's doubt about this anywhere, we could add an example to the docs
and to the PEP.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190305/435f1037/attachment.html>


More information about the Python-ideas mailing list