On 2019-10-18 17:31, Andrew Barnert via Python-ideas wrote:
On Oct 18, 2019, at 06:31, Richard Musil <risa20...@gmail.com> wrote:
Technically we can call set union also an "add" operation, yet we use different semantics to
express it, for exactly the same reason, it is not "arithmetic" nor, "concatenation", but
something different.
Set union had analogies to two different operations that pre-existed it:
concatenation, and bitwise or. Especially since bitwise or is often used
(especially by people who’d rather by writing C than Python) as set union on
integers used as bit sets.
Set union also needs an intersection method (I’m oversimplifying by leaving our
symmetric and asymmetric difference, but I don’t think that affects anything). And
bitwise and is really the only good analogy there (there is no list operation remotely
like intersection). And that pretty much settles it for union: nobody’s going to want +
and &, so it has to be | and &.
Notice that there was no question of adding a whole new operator for set union,
just a question of deciding, of two existing operators, both of which were good
fits, which was a better fit.
Dict merge is in the same situation. It has analogies to concatenation and to set
union. Some people also want to add intersection as well, in which case that would
pretty much settle it: merge is union, and it has to be spelled |. But, unlike with
sets, it isn’t at all obvious that we need intersection (and difference). I think
that question is the main stumbling block to whether + or | is better. But either of
those makes sense, and it’s just down to which of those two is better; there’s no
reason to spell it as >> or ^ or (), or to add a whole new operator and
protocol to Python (or a facility for arbitrary in-language-defined operators).
It's interesting to note:
>>> s = {False}
>>> s |= {0}
>>> s
{False}
so would it be intuitive that when a dict has a matching key its value
is updated?
As a side note:
>>> {False} & {0}
{0}
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/ZFTR7LX5YZCQIACK3H6RR5UPAVJCVNUE/
Code of Conduct: http://python.org/psf/codeofconduct/