I think that PEP 584 – Add Union Operators To dict addresses it (i.e. dictionary part and in general) pretty neatly:
Only One Way To Do It
Dict union will violate the Only One Way koan from the Zen.
Response
There is no such koan. “Only One Way” is a calumny about Python originating long ago from the Perl community.
More Than One Way To Do It
Okay, the Zen doesn’t say that there should be Only One Way To Do It. But it does have a prohibition against allowing “more than one way to do it”.
Response
There is no such prohibition. The “Zen of Python” merely expresses a preference for “only one obvious way”:
There should be one-- and preferably only one --obvious way to do it.
The emphasis here is that there should be an obvious way to do “it”. In the case of dict update operations, there are at least two different operations that we might wish to do:
-
Update a dict in place : The Obvious Way is to use the
update()
method. If this proposal is accepted, the|=
augmented assignment operator will also work, but that is a side-effect of how augmented assignments are defined. Which you choose is a matter of taste. -
Merge two existing dicts into a third, new dict : This PEP proposes that the Obvious Way is to use the
|
merge operator.
In practice, this preference for “only one way” is frequently violated in Python. For example, every for
loop could be re-written as a while
loop; every if
block could be written as an if
/ else
block. List, set and dict comprehensions could all be replaced by generator expressions. Lists offer no fewer than five ways to implement concatenation:
- Concatenation operator:
a + b
- In-place concatenation operator:
a += b
- Slice assignment:
a[len(a):] = b
- Sequence unpacking:
[*a, *b]
- Extend method:
a.extend(b)
We should not be too strict about rejecting useful functionality because it violates “only one way”.