I think the "if unset" behavior is well handled by collections.ChainMap. But I do think that fact should be prominent in any documentation of the new dict Union operator.
On Wed, Feb 26, 2020, 11:06 AM Guido van Rossum <gu...@python.org> wrote: > On Wed, Feb 26, 2020 at 7:43 AM Claudio Jolowicz <cjolow...@gmail.com> > wrote: > >> In my experience, the expression `value |= other` is a common idiom across >> programming languages to provide a default for `value` if it is "unset". >> For a >> container-type, I would expect this operation, informally spoken, to be >> applied >> element-wise. In other words, the resulting dict would have the union of >> keys, >> with values supplied from `other` only if keys are missing in `value`. >> >> This would allow users to provide defaults in a dictionary of settings, >> like >> this: >> >> settings |= defaults >> >> As this usage generalizes a (perceived) common idiom, I would expect >> people to >> get bitten by this. >> > > Interesting. Can you point to specific examples of this? In what other > languages have you seen this? (Not that it would make us change PEP 584, > but if this appears common we could probably warn about it prominently in > docs and tutorials.) > > The pattern I'm familiar with for applying defaults always starts with the > defaults and then adds the local overrides, so the PEP 584 behavior fits > perfectly. (Probably because in Python before PEP 584 the most common > operation is dict.update(), which has the same semantics.) > > -- > --Guido van Rossum (python.org/~guido) > *Pronouns: he/him **(why is my pronoun here?)* > <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/> > _______________________________________________ > Python-Dev mailing list -- python-dev@python.org > To unsubscribe send an email to python-dev-le...@python.org > https://mail.python.org/mailman3/lists/python-dev.python.org/ > Message archived at > https://mail.python.org/archives/list/python-dev@python.org/message/JHXN342STNGSPX3NRAEXRS6JOXH5BCNU/ > Code of Conduct: http://python.org/psf/codeofconduct/ >
_______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/KYVGC2RYPSOXGOHQO5WDQUUE4WJ4TBE7/ Code of Conduct: http://python.org/psf/codeofconduct/