On Wed, Oct 23, 2019 at 7:18 AM Steven D'Aprano <steve@pearwood.info> wrote:
On Tue, Oct 22, 2019 at 11:39:59AM -0700, Mike Miller wrote:
>
> On 2019-10-18 10:23, Ricky Teachey wrote:
> >but i'm -0 because i am very concerned it will not be obvious to new
> >learners, without constantly looking it up, whether adding two mappings
> >together would either:
>
> The big trade off I'm gathering from this mega-thread is that the |, |=
> operators are more accurate, but less obvious to newcomers, who will first
> try +, += instead.

I'm surprised by that description. I don't think it is just newcomers
who either suggest or prefer plus over pipe, and I don't think that pipe
is "more accurate".

Looking at:
```
a = dict()
b = dict()
c = a + b
d = a | b
```
no one can tell what either '+' or '|' does in this context without guessing, because it does do neither "arithmetic addition", nor "concatenation", nor "union set" (or bitwise op, or any other op one could come up). '+' is more familiar or '|' might even be completely new for some users, but this distinction does not help making '+' in this context "more obvious".

What I can only say for sure is that it is an operation on two dicts and the result is possibly again a dict. So consulting the doc will be needed in either case.
 
As I pointed out in the PEP, plus often gets used for non-commutative
operations (such as concatenation and ordinal arithmetic) but I am
unaware of the union operator ∪ or | as spelled in Python ever being
used for a non-commutative operation.

The commutative property of the arithmetic addition is a distinct feature, but not a fundamental one. Both the arithmetic addition and the concatenation (which is not commutative) are different manifestations of the same "generic idea of addition" in two different contexts (systems). This is the reason while intuitively we do not object using '+' in both.

Union set, while being commutative, is not a manifestation of the "addition idea", but of something else - a union set idea :), which is different from the addition idea, because it is not only more specialized, but, and this is the important difference, requires recognition of another concept - identity, which the addition does not need, and on the abstract (idea) level operates differently (vaguely saying, "addition" preserves the integrity, "union set" preserves the identity).

Contrary to the views of many people upset that dict + would be non-
commutative, it is arguably *more natural* to use plus for a non-
commutative operation than it would be to use pipe.

If the aim is to get an operator (a graphical symbol) which is the closest representation of the _idea_ then the commutativity is not a deciding factor (as I mentioned above), but only a distinctive attribute, which may differ in different manifestations of the same idea.

The dict merge (or update) operation is unfortunately related neither to all "addition manifestations" nor to "union set" in a sense that it comes from the same idea, but it is an idea on its own, because apart from the recognition of identity (which union set requires), it also requires a concept of association (which neither addition nor union set use or need) and the actual operation is again different from the former two.

The biggest advantage of pipe is that it naturally lends itself to the
rest of the set operations. But using pipe for a non-commutative
operator is far less common than using plus.

Using either '|' or '+' is technically not correct, if we want to have it consistent on the abstract level. But '|' (the idea of what '|' is representing) is more similar to what is happening in dict merge than the addition idea. Since introducing a new (operator) symbol for that seems to be unacceptable (for practical reasons), '|' is the best candidate we have.

Richard