2019-03-01 11:50:50 -05:00
|
|
|
PEP: 584
|
2020-02-04 18:21:25 -05:00
|
|
|
Title: Add Union Operators To dict
|
2019-03-01 11:50:50 -05:00
|
|
|
Version: $Revision$
|
|
|
|
Last-Modified: $Date$
|
2019-10-16 20:41:41 -04:00
|
|
|
Author: Steven D'Aprano <steve@pearwood.info>,
|
|
|
|
Brandt Bucher <brandtbucher@gmail.com>
|
2020-02-04 18:21:25 -05:00
|
|
|
BDFL-Delegate: Guido van Rossum <guido@python.org>
|
2020-06-18 21:49:52 -04:00
|
|
|
Status: Final
|
2019-03-01 11:50:50 -05:00
|
|
|
Type: Standards Track
|
|
|
|
Content-Type: text/x-rst
|
|
|
|
Created: 01-Mar-2019
|
2020-02-04 18:21:25 -05:00
|
|
|
Python-Version: 3.9
|
2020-06-18 21:49:52 -04:00
|
|
|
Post-History: 01-Mar-2019, 16-Oct-2019, 02-Dec-2019, 04-Feb-2020,
|
|
|
|
17-Feb-2020
|
|
|
|
Resolution: https://mail.python.org/archives/list/python-dev@python.org/thread/6KT2KIOTYXMDCD2CCAOLOI7LUGTN6MBS
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
|
|
|
|
========
|
2019-03-01 11:50:50 -05:00
|
|
|
Abstract
|
2020-02-04 18:21:25 -05:00
|
|
|
========
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
This PEP proposes adding merge (``|``) and update (``|=``) operators
|
|
|
|
to the built-in ``dict`` class.
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-03-14 16:03:12 -04:00
|
|
|
.. note::
|
|
|
|
After this PEP was accepted, the decision was made to also
|
|
|
|
implement the new operators for `several other standard library
|
|
|
|
mappings <https://bugs.python.org/issue36144>`_.
|
|
|
|
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
==========
|
|
|
|
Motivation
|
|
|
|
==========
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
The current ways to merge two dicts have several disadvantages:
|
2019-03-01 11:50:50 -05:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
---------------
|
|
|
|
``dict.update``
|
|
|
|
---------------
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
``d1.update(d2)`` modifies ``d1`` in-place.
|
|
|
|
``e = d1.copy(); e.update(d2)`` is not an expression and needs a
|
|
|
|
temporary variable.
|
2019-03-01 11:50:50 -05:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
----------------
|
|
|
|
``{**d1, **d2}``
|
|
|
|
----------------
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Dict unpacking looks ugly and is not easily discoverable. Few people
|
|
|
|
would be able to guess what it means the first time they see it, or
|
|
|
|
think of it as the "obvious way" to merge two dicts.
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
`As Guido said
|
|
|
|
<https://mail.python.org/archives/list/python-ideas@python.org/message/K4IC74IXE23K4KEL7OUFK3VBC62HGGVF/>`_:
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
I'm sorry for PEP 448, but even if you know about ``**d`` in
|
|
|
|
simpler contexts, if you were to ask a typical Python user how
|
|
|
|
to combine two dicts into a new one, I doubt many people would
|
|
|
|
think of ``{**d1, **d2}``. I know I myself had forgotten about
|
|
|
|
it when this thread started!
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
``{**d1, **d2}`` ignores the types of the mappings and always returns
|
|
|
|
a ``dict``. ``type(d1)({**d1, **d2})`` fails for dict subclasses
|
|
|
|
such as ``defaultdict`` that have an incompatible ``__init__`` method.
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
------------------------
|
|
|
|
``collections.ChainMap``
|
|
|
|
------------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
``ChainMap`` is unfortunately poorly-known and doesn't qualify as
|
|
|
|
"obvious". It also resolves duplicate keys in the opposite order to
|
|
|
|
that expected ("first seen wins" instead of "last seen wins"). Like
|
|
|
|
dict unpacking, it is tricky to get it to honor the desired subclass.
|
|
|
|
For the same reason, ``type(d1)(ChainMap(d2, d1))`` fails for some
|
|
|
|
subclasses of dict.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Further, ChainMaps wrap their underlying dicts, so writes to the
|
|
|
|
ChainMap will modify the original dict::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
>>> d1 = {'spam': 1}
|
|
|
|
>>> d2 = {'eggs': 2}
|
|
|
|
>>> merged = ChainMap(d2, d1)
|
|
|
|
>>> merged['eggs'] = 999
|
|
|
|
>>> d2
|
|
|
|
{'eggs': 999}
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-17 15:07:40 -05:00
|
|
|
------------------
|
|
|
|
``dict(d1, **d2)``
|
|
|
|
------------------
|
|
|
|
|
|
|
|
This "neat trick" is not well-known, and only works when ``d2`` is
|
|
|
|
entirely string-keyed::
|
|
|
|
|
|
|
|
>>> d1 = {"spam": 1}
|
|
|
|
>>> d2 = {3665: 2}
|
|
|
|
>>> dict(d1, **d2)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
TypeError: keywords must be strings
|
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
=========
|
|
|
|
Rationale
|
|
|
|
=========
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
The new operators will have the same relationship to the
|
|
|
|
``dict.update`` method as the list concatenate (``+``) and extend
|
|
|
|
(``+=``) operators have to ``list.extend``. Note that this is
|
|
|
|
somewhat different from the relationship that ``|``/``|=`` have with
|
|
|
|
``set.update``; the authors have determined that allowing the in-place
|
2020-02-17 15:07:40 -05:00
|
|
|
operator to accept a wider range of types (as ``list`` does) is a more
|
|
|
|
useful design, and that restricting the types of the binary operator's
|
|
|
|
operands (again, as ``list`` does) will help avoid silent errors
|
|
|
|
caused by complicated implicit type casting on both sides.
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Key conflicts will be resolved by keeping the rightmost value. This
|
|
|
|
matches the existing behavior of similar ``dict`` operations, where
|
|
|
|
the last seen value always wins::
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
{'a': 1, 'a': 2}
|
|
|
|
{**d, **e}
|
|
|
|
d.update(e)
|
|
|
|
d[k] = v
|
|
|
|
{k: v for x in (d, e) for (k, v) in x.items()}
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
All of the above follow the same rule. This PEP takes the position
|
|
|
|
that this behavior is simple, obvious, usually the behavior we want,
|
|
|
|
and should be the default behavior for dicts. This means that dict
|
|
|
|
union is not commutative; in general ``d | e != e | d``.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Similarly, the *iteration order* of the key-value pairs in the
|
|
|
|
dictionary will follow the same semantics as the examples above, with
|
2020-02-17 15:07:40 -05:00
|
|
|
each newly added key (and its value) being appended to the current
|
|
|
|
sequence.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
=============
|
|
|
|
Specification
|
|
|
|
=============
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-17 15:07:40 -05:00
|
|
|
Dict union will return a new ``dict`` consisting of the left operand
|
|
|
|
merged with the right operand, each of which must be a ``dict`` (or an
|
|
|
|
instance of a ``dict`` subclass). If a key appears in both operands,
|
|
|
|
the last-seen value (i.e. that from the right-hand operand) wins::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
>>> d = {'spam': 1, 'eggs': 2, 'cheese': 3}
|
|
|
|
>>> e = {'cheese': 'cheddar', 'aardvark': 'Ethel'}
|
|
|
|
>>> d | e
|
|
|
|
{'spam': 1, 'eggs': 2, 'cheese': 'cheddar', 'aardvark': 'Ethel'}
|
|
|
|
>>> e | d
|
|
|
|
{'aardvark': 'Ethel', 'spam': 1, 'eggs': 2, 'cheese': 3}
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
The augmented assignment version operates in-place::
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
>>> d |= e
|
|
|
|
>>> d
|
|
|
|
{'spam': 1, 'eggs': 2, 'cheese': 'cheddar', 'aardvark': 'Ethel'}
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Augmented assignment behaves identically to the ``update`` method
|
|
|
|
called with a single positional argument, so it also accepts anything
|
|
|
|
implementing the Mapping protocol (more specifically, anything with
|
|
|
|
the ``keys`` and ``__getitem__`` methods) or iterables of key-value
|
|
|
|
pairs. This is analogous to ``list +=`` and ``list.extend``, which
|
|
|
|
accept any iterable, not just lists. Continued from above::
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
>>> d | [('spam', 999)]
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
TypeError: can only merge dict (not "list") to dict
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
>>> d |= [('spam', 999)]
|
|
|
|
>>> d
|
|
|
|
{'eggs': 2, 'cheese': 'cheddar', 'aardvark': 'Ethel', 'spam': 999}
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-05-20 12:34:00 -04:00
|
|
|
When new keys are added, their order matches their order within the
|
|
|
|
right-hand mapping, if any exists for its type.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
========================
|
|
|
|
Reference Implementation
|
|
|
|
========================
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-06-18 21:49:52 -04:00
|
|
|
One of the authors has `written a C implementation
|
2020-02-24 20:43:06 -05:00
|
|
|
<https://github.com/python/cpython/pull/12088>`_.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
An *approximate* pure-Python implementation is::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
def __or__(self, other):
|
|
|
|
if not isinstance(other, dict):
|
|
|
|
return NotImplemented
|
2020-02-17 15:07:40 -05:00
|
|
|
new = dict(self)
|
2020-02-04 18:21:25 -05:00
|
|
|
new.update(other)
|
|
|
|
return new
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
def __ror__(self, other):
|
|
|
|
if not isinstance(other, dict):
|
|
|
|
return NotImplemented
|
2020-02-17 15:07:40 -05:00
|
|
|
new = dict(other)
|
2020-02-04 18:21:25 -05:00
|
|
|
new.update(self)
|
|
|
|
return new
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
def __ior__(self, other):
|
2020-02-17 15:07:40 -05:00
|
|
|
dict.update(self, other)
|
2020-02-04 18:21:25 -05:00
|
|
|
return self
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
================
|
|
|
|
Major Objections
|
|
|
|
================
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
-----------------------------
|
|
|
|
Dict Union Is Not Commutative
|
|
|
|
-----------------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Union is commutative, but dict union will not be (``d | e != e | d``).
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
''''''''
|
|
|
|
Response
|
|
|
|
''''''''
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
There is precedent for non-commutative unions in Python::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
>>> {0} | {False}
|
|
|
|
{0}
|
|
|
|
>>> {False} | {0}
|
|
|
|
{False}
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
While the results may be equal, they are distinctly different. In
|
|
|
|
general, ``a | b`` is not the same operation as ``b | a``.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
------------------------------
|
|
|
|
Dict Union Will Be Inefficient
|
|
|
|
------------------------------
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Giving a pipe operator to mappings is an invitation to writing code
|
|
|
|
that doesn't scale well. Repeated dict union is inefficient:
|
|
|
|
``d | e | f | g | h`` creates and destroys three temporary mappings.
|
2019-03-01 11:50:50 -05:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
''''''''
|
|
|
|
Response
|
|
|
|
''''''''
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
The same argument applies to sequence concatenation.
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Sequence concatenation grows with the total number of items in the
|
|
|
|
sequences, leading to O(N**2) (quadratic) performance. Dict union is
|
|
|
|
likely to involve duplicate keys, so the temporary mappings will
|
|
|
|
not grow as fast.
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Just as it is rare for people to concatenate large numbers of lists or
|
|
|
|
tuples, the authors of this PEP believe that it will be rare for
|
|
|
|
people to merge large numbers of dicts. ``collections.Counter`` is a
|
|
|
|
dict subclass that supports many operators, and there are no known
|
|
|
|
examples of people having performance issues due to combining large
|
|
|
|
numbers of Counters. Further, a survey of the standard library by the
|
|
|
|
authors found no examples of merging more than two dicts, so this is
|
|
|
|
unlikely to be a performance problem in practice... "Everything is
|
|
|
|
fast for small enough N".
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
If one expects to be merging a large number of dicts where performance
|
|
|
|
is an issue, it may be better to use an explicit loop and in-place
|
|
|
|
merging::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
new = {}
|
|
|
|
for d in many_dicts:
|
|
|
|
new |= d
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
-------------------
|
|
|
|
Dict Union Is Lossy
|
|
|
|
-------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Dict union can lose data (values may disappear); no other form of
|
|
|
|
union is lossy.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
''''''''
|
|
|
|
Response
|
|
|
|
''''''''
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
It isn't clear why the first part of this argument is a problem.
|
|
|
|
``dict.update()`` may throw away values, but not keys; that is
|
|
|
|
expected behavior, and will remain expected behavior regardless of
|
|
|
|
whether it is spelled as ``update()`` or ``|``.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Other types of union are also lossy, in the sense of not being
|
|
|
|
reversable; you cannot get back the two operands given only the union.
|
|
|
|
``a | b == 365``... what are ``a`` and ``b``?
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
---------------------
|
|
|
|
Only One Way To Do It
|
|
|
|
---------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Dict union will violate the Only One Way koan from the Zen.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
''''''''
|
|
|
|
Response
|
|
|
|
''''''''
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
There is no such koan. "Only One Way" is a calumny about Python
|
|
|
|
originating long ago from the Perl community.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
--------------------------
|
|
|
|
More Than One Way To Do It
|
|
|
|
--------------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
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".
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
''''''''
|
|
|
|
Response
|
|
|
|
''''''''
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
There is no such prohibition. The "Zen of Python" merely expresses a
|
|
|
|
*preference* for "only one *obvious* way"::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
There should be one-- and preferably only one --obvious way to do
|
|
|
|
it.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
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:
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
- *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.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
- *Merge two existing dicts into a third, new dict*: This PEP proposes
|
|
|
|
that the Obvious Way is to use the ``|`` merge operator.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
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:
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
- 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)``
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
We should not be too strict about rejecting useful functionality
|
|
|
|
because it violates "only one way".
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
------------------------------------------
|
|
|
|
Dict Union Makes Code Harder To Understand
|
|
|
|
------------------------------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Dict union makes it harder to tell what code means. To paraphrase the
|
|
|
|
objection rather than quote anyone in specific: "If I see
|
|
|
|
``spam | eggs``, I can't tell what it does unless I know what ``spam``
|
|
|
|
and ``eggs`` are".
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
''''''''
|
|
|
|
Response
|
|
|
|
''''''''
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
This is very true. But it is equally true today, where the use of the
|
|
|
|
``|`` operator could mean any of:
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-05 14:14:58 -05:00
|
|
|
- ``int``/``bool`` bitwise-or
|
|
|
|
- ``set``/``frozenset`` union
|
|
|
|
- any other overloaded operation
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Adding dict union to the set of possibilities doesn't seem to make
|
|
|
|
it *harder* to understand the code. No more work is required to
|
|
|
|
determine that ``spam`` and ``eggs`` are mappings than it would take
|
|
|
|
to determine that they are sets, or integers. And good naming
|
|
|
|
conventions will help::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
flags |= WRITEABLE # Probably numeric bitwise-or.
|
|
|
|
DO_NOT_RUN = WEEKENDS | HOLIDAYS # Probably set union.
|
|
|
|
settings = DEFAULT_SETTINGS | user_settings | workspace_settings # Probably dict union.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
--------------------------------
|
|
|
|
What About The Full ``set`` API?
|
|
|
|
--------------------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
dicts are "set like", and should support the full collection of set
|
|
|
|
operators: ``|``, ``&``, ``^``, and ``-``.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
''''''''
|
|
|
|
Response
|
|
|
|
''''''''
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
This PEP does not take a position on whether dicts should support the
|
|
|
|
full collection of set operators, and would prefer to leave that for a
|
|
|
|
later PEP (one of the authors is interested in drafting such a PEP).
|
|
|
|
For the benefit of any later PEP, a brief summary follows.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Set symmetric difference (``^``) is obvious and natural. For example,
|
|
|
|
given two dicts::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
d1 = {"spam": 1, "eggs": 2}
|
|
|
|
d2 = {"ham": 3, "eggs": 4}
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
the symmetric difference ``d1 ^ d2`` would be
|
|
|
|
``{"spam": 1, "ham": 3}``.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Set difference (``-``) is also obvious and natural, and an earlier
|
|
|
|
version of this PEP included it in the proposal. Given the dicts
|
|
|
|
above, we would have ``d1 - d2`` be ``{"spam": 1}`` and ``d2 - d1`` be
|
|
|
|
``{"ham": 3}``.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Set intersection (``&``) is a bit more problematic. While it is easy
|
|
|
|
to determine the intersection of *keys* in two dicts, it is not clear
|
|
|
|
what to do with the *values*. Given the two dicts above, it is
|
|
|
|
obvious that the only key of ``d1 & d2`` must be ``"eggs"``. "Last
|
|
|
|
seen wins", however, has the advantage of consistency with other dict
|
|
|
|
operations (and the proposed union operators).
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-03-14 16:03:12 -04:00
|
|
|
----------------------------------------------
|
|
|
|
What About ``Mapping`` And ``MutableMapping``?
|
|
|
|
----------------------------------------------
|
|
|
|
|
|
|
|
``collections.abc.Mapping`` and ``collections.abc.MutableMapping``
|
|
|
|
should define ``|`` and ``|=``, so subclasses could just inherit the
|
|
|
|
new operators instead of having to define them.
|
|
|
|
|
|
|
|
|
|
|
|
''''''''
|
|
|
|
Response
|
|
|
|
''''''''
|
|
|
|
|
|
|
|
There are two primary reasons why adding the new operators to these
|
|
|
|
classes would be problematic:
|
|
|
|
|
|
|
|
- Currently, neither defines a ``copy`` method, which would be
|
|
|
|
necessary for ``|`` to create a new instance.
|
|
|
|
|
|
|
|
- Adding ``|=`` to ``MutableMapping`` (or a ``copy`` method to
|
|
|
|
``Mapping``) would create compatibility issues for virtual
|
|
|
|
subclasses.
|
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
==============
|
|
|
|
Rejected Ideas
|
|
|
|
==============
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
------------------
|
|
|
|
Rejected Semantics
|
|
|
|
------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
There were at least four other proposed solutions for handling
|
|
|
|
conflicting keys. These alternatives are left to subclasses of dict.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
'''''
|
|
|
|
Raise
|
|
|
|
'''''
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
It isn't clear that this behavior has many use-cases or will be often
|
|
|
|
useful, but it will likely be annoying as any use of the dict union
|
|
|
|
operator would have to be guarded with a ``try``/``except`` clause.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
''''''''''''''''''''''''''''''''''''''''''''
|
|
|
|
Add The Values (As Counter Does, with ``+``)
|
|
|
|
''''''''''''''''''''''''''''''''''''''''''''
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Too specialised to be used as the default behavior.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
''''''''''''''''''''''''''''''''
|
|
|
|
Leftmost Value (First-Seen) Wins
|
|
|
|
''''''''''''''''''''''''''''''''
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
It isn't clear that this behavior has many use-cases. In fact, one
|
|
|
|
can simply reverse the order of the arguments::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
d2 | d1 # d1 merged with d2, keeping existing values in d1
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
''''''''''''''''''''''''''''
|
|
|
|
Concatenate Values In A List
|
|
|
|
''''''''''''''''''''''''''''
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
This is likely to be too specialised to be the default. It is not
|
|
|
|
clear what to do if the values are already lists::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
{'a': [1, 2]} | {'a': [3, 4]}
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Should this give ``{'a': [1, 2, 3, 4]}`` or
|
|
|
|
``{'a': [[1, 2], [3, 4]]}``?
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
---------------------
|
|
|
|
Rejected Alternatives
|
|
|
|
---------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
'''''''''''''''''''''''''
|
|
|
|
Use The Addition Operator
|
|
|
|
'''''''''''''''''''''''''
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
This PEP originally started life as a proposal for dict addition,
|
|
|
|
using the ``+`` and ``+=`` operator. That choice proved to be
|
|
|
|
exceedingly controversial, with many people having serious objections
|
|
|
|
to the choice of operator. For details, see `previous versions
|
|
|
|
<https://github.com/python/peps/commits/master/pep-0584.rst>`_ of the
|
|
|
|
PEP and the mailing list discussions_.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
'''''''''''''''''''''''''''
|
|
|
|
Use The Left Shift Operator
|
|
|
|
'''''''''''''''''''''''''''
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
The ``<<`` operator didn't seem to get much support on Python-Ideas,
|
|
|
|
but no major objections either. Perhaps the strongest objection was
|
|
|
|
Chris Angelico's comment
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
The "cuteness" value of abusing the operator to indicate
|
|
|
|
information flow got old shortly after C++ did it.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
'''''''''''''''''''''''''''''
|
|
|
|
Use A New Left Arrow Operator
|
|
|
|
'''''''''''''''''''''''''''''
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Another suggestion was to create a new operator ``<-``. Unfortunately
|
|
|
|
this would be ambiguous, ``d <- e`` could mean ``d merge e`` or
|
|
|
|
``d less-than minus e``.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
''''''''''''
|
|
|
|
Use A Method
|
|
|
|
''''''''''''
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
A ``dict.merged()`` method would avoid the need for an operator at
|
|
|
|
all. One subtlety is that it would likely need slightly different
|
|
|
|
implementations when called as an unbound method versus as a bound
|
|
|
|
method.
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
As an unbound method, the behavior could be similar to::
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
def merged(cls, *mappings, **kw):
|
|
|
|
new = cls() # Will this work for defaultdict?
|
|
|
|
for m in mappings:
|
|
|
|
new.update(m)
|
|
|
|
new.update(kw)
|
|
|
|
return new
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
As a bound method, the behavior could be similar to::
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
def merged(self, *mappings, **kw):
|
2019-10-16 20:41:41 -04:00
|
|
|
new = self.copy()
|
2020-02-04 18:21:25 -05:00
|
|
|
for m in mappings:
|
|
|
|
new.update(m)
|
|
|
|
new.update(kw)
|
2019-10-16 20:41:41 -04:00
|
|
|
return new
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Advantages
|
|
|
|
==========
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
* Arguably, methods are more discoverable than operators.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
* The method could accept any number of positional and keyword
|
|
|
|
arguments, avoiding the inefficiency of creating temporary dicts.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
* Accepts sequences of ``(key, value)`` pairs like the ``update``
|
|
|
|
method.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
* Being a method, it is easily to override in a subclass if you need
|
|
|
|
alternative behaviors such as "first wins", "unique keys", etc.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Disadvantages
|
|
|
|
=============
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
* Would likely require a new kind of method decorator which combined
|
|
|
|
the behavior of regular instance methods and ``classmethod``. It
|
|
|
|
would need to be public (but not necessarily a builtin) for those
|
|
|
|
needing to override the method. There is a
|
|
|
|
`proof of concept <http://code.activestate.com/recipes/577030>`_.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
* It isn't an operator. Guido discusses `why operators are useful
|
|
|
|
<https://mail.python.org/archives/list/python-ideas@python.org/message/52DLME5DKNZYFEETCTRENRNKWJ2B4DD5/>`_.
|
|
|
|
For another viewpoint, see `Nick Coghlan's blog post
|
|
|
|
<https://www.curiousefficiency.org/posts/2019/03/what-does-x-equals-a-plus-b-mean.html>`_.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
''''''''''''''
|
|
|
|
Use a Function
|
|
|
|
''''''''''''''
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Instead of a method, use a new built-in function ``merged()``. One
|
|
|
|
possible implementation could be something like this::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
def merged(*mappings, **kw):
|
|
|
|
if mappings and isinstance(mappings[0], dict):
|
|
|
|
# If the first argument is a dict, use its type.
|
|
|
|
new = mappings[0].copy()
|
|
|
|
mappings = mappings[1:]
|
|
|
|
else:
|
|
|
|
# No positional arguments, or the first argument is a
|
|
|
|
# sequence of (key, value) pairs.
|
|
|
|
new = dict()
|
|
|
|
for m in mappings:
|
|
|
|
new.update(m)
|
|
|
|
new.update(kw)
|
|
|
|
return new
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
An alternative might be to forgo the arbitrary keywords, and take a
|
|
|
|
single keyword parameter that specifies the behavior on collisions::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
def merged(*mappings, on_collision=lambda k, v1, v2: v2):
|
|
|
|
# implementation left as an exercise to the reader
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Advantages
|
|
|
|
==========
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
* Most of the same advantages of the method solutions above.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
* Doesn't require a subclass to implement alternative behavior on
|
|
|
|
collisions, just a function.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Disadvantages
|
|
|
|
=============
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
* May not be important enough to be a builtin.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
* Hard to override behavior if you need something like "first wins",
|
|
|
|
without losing the ability to process arbitrary keyword arguments.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
========
|
|
|
|
Examples
|
|
|
|
========
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
The authors of this PEP did a survey of third party libraries for
|
|
|
|
dictionary merging which might be candidates for dict union.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
This is a cursory list based on a subset of whatever arbitrary
|
|
|
|
third-party packages happened to be installed on one of the authors'
|
|
|
|
computers, and may not reflect the current state of any package. Also
|
|
|
|
note that, while further (unrelated) refactoring may be possible, the
|
|
|
|
rewritten version only adds usage of the new operators for an
|
|
|
|
apples-to-apples comparison. It also reduces the result to an
|
|
|
|
expression when it is efficient to do so.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
-----------------------
|
|
|
|
IPython/zmq/ipkernel.py
|
|
|
|
-----------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Before::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
aliases = dict(kernel_aliases)
|
|
|
|
aliases.update(shell_aliases)
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
After::
|
|
|
|
|
|
|
|
aliases = kernel_aliases | shell_aliases
|
|
|
|
|
|
|
|
|
|
|
|
------------------------
|
|
|
|
IPython/zmq/kernelapp.py
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
Before::
|
|
|
|
|
|
|
|
kernel_aliases = dict(base_aliases)
|
|
|
|
kernel_aliases.update({
|
|
|
|
'ip' : 'KernelApp.ip',
|
|
|
|
'hb' : 'KernelApp.hb_port',
|
|
|
|
'shell' : 'KernelApp.shell_port',
|
|
|
|
'iopub' : 'KernelApp.iopub_port',
|
|
|
|
'stdin' : 'KernelApp.stdin_port',
|
|
|
|
'parent': 'KernelApp.parent',
|
|
|
|
})
|
|
|
|
if sys.platform.startswith('win'):
|
|
|
|
kernel_aliases['interrupt'] = 'KernelApp.interrupt'
|
|
|
|
|
|
|
|
kernel_flags = dict(base_flags)
|
|
|
|
kernel_flags.update({
|
|
|
|
'no-stdout' : (
|
|
|
|
{'KernelApp' : {'no_stdout' : True}},
|
|
|
|
"redirect stdout to the null device"),
|
|
|
|
'no-stderr' : (
|
|
|
|
{'KernelApp' : {'no_stderr' : True}},
|
|
|
|
"redirect stderr to the null device"),
|
|
|
|
})
|
|
|
|
|
|
|
|
After::
|
|
|
|
|
|
|
|
kernel_aliases = base_aliases | {
|
|
|
|
'ip' : 'KernelApp.ip',
|
|
|
|
'hb' : 'KernelApp.hb_port',
|
|
|
|
'shell' : 'KernelApp.shell_port',
|
|
|
|
'iopub' : 'KernelApp.iopub_port',
|
|
|
|
'stdin' : 'KernelApp.stdin_port',
|
|
|
|
'parent': 'KernelApp.parent',
|
|
|
|
}
|
|
|
|
if sys.platform.startswith('win'):
|
|
|
|
kernel_aliases['interrupt'] = 'KernelApp.interrupt'
|
|
|
|
|
|
|
|
kernel_flags = base_flags | {
|
|
|
|
'no-stdout' : (
|
|
|
|
{'KernelApp' : {'no_stdout' : True}},
|
|
|
|
"redirect stdout to the null device"),
|
|
|
|
'no-stderr' : (
|
|
|
|
{'KernelApp' : {'no_stderr' : True}},
|
|
|
|
"redirect stderr to the null device"),
|
|
|
|
}
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
----------------------------------
|
|
|
|
matplotlib/backends/backend_svg.py
|
|
|
|
----------------------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Before::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
attrib = attrib.copy()
|
|
|
|
attrib.update(extra)
|
|
|
|
attrib = attrib.items()
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
After::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
attrib = (attrib | extra).items()
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
----------------------------------
|
|
|
|
matplotlib/delaunay/triangulate.py
|
|
|
|
----------------------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Before::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
edges = {}
|
|
|
|
edges.update(dict(zip(self.triangle_nodes[border[:,0]][:,1],
|
|
|
|
self.triangle_nodes[border[:,0]][:,2])))
|
|
|
|
edges.update(dict(zip(self.triangle_nodes[border[:,1]][:,2],
|
|
|
|
self.triangle_nodes[border[:,1]][:,0])))
|
|
|
|
edges.update(dict(zip(self.triangle_nodes[border[:,2]][:,0],
|
|
|
|
self.triangle_nodes[border[:,2]][:,1])))
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Rewrite as::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
edges = {}
|
|
|
|
edges |= zip(self.triangle_nodes[border[:,0]][:,1],
|
|
|
|
self.triangle_nodes[border[:,0]][:,2])
|
|
|
|
edges |= zip(self.triangle_nodes[border[:,1]][:,2],
|
|
|
|
self.triangle_nodes[border[:,1]][:,0])
|
|
|
|
edges |= zip(self.triangle_nodes[border[:,2]][:,0],
|
|
|
|
self.triangle_nodes[border[:,2]][:,1])
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
--------------------
|
|
|
|
matplotlib/legend.py
|
|
|
|
--------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Before::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
hm = default_handler_map.copy()
|
|
|
|
hm.update(self._handler_map)
|
|
|
|
return hm
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
After::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
return default_handler_map | self._handler_map
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
----------------
|
|
|
|
numpy/ma/core.py
|
|
|
|
----------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Before::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
_optinfo = {}
|
|
|
|
_optinfo.update(getattr(obj, '_optinfo', {}))
|
|
|
|
_optinfo.update(getattr(obj, '_basedict', {}))
|
|
|
|
if not isinstance(obj, MaskedArray):
|
|
|
|
_optinfo.update(getattr(obj, '__dict__', {}))
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
After::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
_optinfo = {}
|
|
|
|
_optinfo |= getattr(obj, '_optinfo', {})
|
|
|
|
_optinfo |= getattr(obj, '_basedict', {})
|
|
|
|
if not isinstance(obj, MaskedArray):
|
|
|
|
_optinfo |= getattr(obj, '__dict__', {})
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
----------------
|
|
|
|
praw/internal.py
|
|
|
|
----------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Before::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
data = {'name': six.text_type(user), 'type': relationship}
|
|
|
|
data.update(kwargs)
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
After::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
data = {'name': six.text_type(user), 'type': relationship} | kwargs
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
-----------------
|
|
|
|
pygments/lexer.py
|
|
|
|
-----------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Before::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
kwargs.update(lexer.options)
|
|
|
|
lx = lexer.__class__(**kwargs)
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
After::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
lx = lexer.__class__(**(kwargs | lexer.options))
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
--------------------
|
|
|
|
requests/sessions.py
|
|
|
|
--------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Before::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
merged_setting = dict_class(to_key_val_list(session_setting))
|
|
|
|
merged_setting.update(to_key_val_list(request_setting))
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
After::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
merged_setting = dict_class(to_key_val_list(session_setting)) | to_key_val_list(request_setting)
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
--------------------------
|
|
|
|
sphinx/domains/__init__.py
|
|
|
|
--------------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Before::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
self.attrs = self.known_attrs.copy()
|
|
|
|
self.attrs.update(attrs)
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
After::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
self.attrs = self.known_attrs | attrs
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
---------------------
|
|
|
|
sphinx/ext/doctest.py
|
|
|
|
---------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Before::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
new_opt = code[0].options.copy()
|
|
|
|
new_opt.update(example.options)
|
|
|
|
example.options = new_opt
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
After::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
example.options = code[0].options | example.options
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
---------------------------------
|
|
|
|
sphinx/ext/inheritance_diagram.py
|
|
|
|
---------------------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Before::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
n_attrs = self.default_node_attrs.copy()
|
|
|
|
e_attrs = self.default_edge_attrs.copy()
|
|
|
|
g_attrs.update(graph_attrs)
|
|
|
|
n_attrs.update(node_attrs)
|
|
|
|
e_attrs.update(edge_attrs)
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
After::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
g_attrs |= graph_attrs
|
|
|
|
n_attrs = self.default_node_attrs | node_attrs
|
|
|
|
e_attrs = self.default_edge_attrs | edge_attrs
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
----------------------
|
|
|
|
sphinx/highlighting.py
|
|
|
|
----------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Before::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
kwargs.update(self.formatter_args)
|
|
|
|
return self.formatter(**kwargs)
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
After::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
return self.formatter(**(kwargs | self.formatter_args))
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
--------------------
|
|
|
|
sphinx/quickstart.py
|
|
|
|
--------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Before::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
d2 = DEFAULT_VALUE.copy()
|
|
|
|
d2.update(dict(("ext_"+ext, False) for ext in EXTENSIONS))
|
|
|
|
d2.update(d)
|
|
|
|
d = d2
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
After::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
d = DEFAULT_VALUE | dict(("ext_"+ext, False) for ext in EXTENSIONS) | d
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
------------
|
|
|
|
sympy/abc.py
|
|
|
|
------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Before::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
clash = {}
|
|
|
|
clash.update(clash1)
|
|
|
|
clash.update(clash2)
|
|
|
|
return clash1, clash2, clash
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
After::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
return clash1, clash2, clash1 | clash2
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
-----------------------
|
|
|
|
sympy/parsing/maxima.py
|
|
|
|
-----------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Before::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
dct = MaximaHelpers.__dict__.copy()
|
|
|
|
dct.update(name_dict)
|
|
|
|
obj = sympify(str, locals=dct)
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
After::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
obj = sympify(str, locals=MaximaHelpers.__dict__|name_dict)
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
---------------------------------------------------
|
|
|
|
sympy/printing/ccode.py and sympy/printing/fcode.py
|
|
|
|
---------------------------------------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Before::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
self.known_functions = dict(known_functions)
|
|
|
|
userfuncs = settings.get('user_functions', {})
|
|
|
|
self.known_functions.update(userfuncs)
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
After::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
self.known_functions = known_functions | settings.get('user_functions', {})
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
---------------------------
|
|
|
|
sympy/utilities/runtests.py
|
|
|
|
---------------------------
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Before::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
globs = globs.copy()
|
|
|
|
if extraglobs is not None:
|
|
|
|
globs.update(extraglobs)
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
After::
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
globs = globs | (extraglobs if extraglobs is not None else {})
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
The above examples show that sometimes the ``|`` operator leads to a
|
|
|
|
clear increase in readability, reducing the number of lines of code
|
|
|
|
and improving clarity. However other examples using the ``|``
|
|
|
|
operator lead to long, complex single expressions, possibly well over
|
|
|
|
the PEP 8 maximum line length of 80 columns. As with any other
|
|
|
|
language feature, the programmer should use their own judgement about
|
|
|
|
whether ``|`` improves their code.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
===================
|
|
|
|
Related Discussions
|
|
|
|
===================
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
.. _discussions:
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Mailing list threads (this is by no means an exhaustive list):
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
* `Dict joining using + and +=
|
|
|
|
<https://mail.python.org/archives/list/python-ideas@python.org/thread/BHIJX6MHGMMD3S6D7GVTPZQL4N5V7T42/>`_
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
* `PEP: Dict addition and subtraction
|
|
|
|
<https://mail.python.org/archives/list/python-ideas@python.org/thread/KLDQAPOIJEANCKYCHQZ536WHQ45I6UVW/>`_
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
* `PEP 584: Add + and += operators to the built-in dict class.
|
|
|
|
<https://mail.python.org/archives/list/python-ideas@python.org/thread/W2FCSC3JDA7NUBXAVSTVCUDEGAKWWPTH/>`_
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
* `Moving PEP 584 forward (dict + and += operators)
|
|
|
|
<https://mail.python.org/archives/list/python-ideas@python.org/thread/SWBLMTNQXNL3O5LN3327IYNPFIL2QSH5/>`_
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-05 14:14:58 -05:00
|
|
|
* `PEP 584: Add Union Operators To dict
|
|
|
|
<https://mail.python.org/archives/list/python-dev@python.org/thread/TTIKCDIPC2CDHX23Y57CPHDSVYOWCCER/>`_
|
|
|
|
|
2020-02-24 20:43:06 -05:00
|
|
|
* `Accepting PEP 584: Add Union Operators To dict
|
|
|
|
<https://mail.python.org/archives/list/python-dev@python.org/thread/6KT2KIOTYXMDCD2CCAOLOI7LUGTN6MBS>`_
|
|
|
|
|
2019-03-01 11:50:50 -05:00
|
|
|
`Ticket on the bug tracker <https://bugs.python.org/issue36144>`_
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Merging two dictionaries in an expression is a frequently requested
|
|
|
|
feature. For example:
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
https://stackoverflow.com/questions/38987/how-to-merge-two-dictionaries-in-a-single-expression
|
|
|
|
|
|
|
|
https://stackoverflow.com/questions/1781571/how-to-concatenate-two-dictionaries-to-create-a-new-one-in-python
|
|
|
|
|
|
|
|
https://stackoverflow.com/questions/6005066/adding-dictionaries-together-python
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
Occasionally people request alternative behavior for the merge:
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
https://stackoverflow.com/questions/1031199/adding-dictionaries-in-python
|
|
|
|
|
|
|
|
https://stackoverflow.com/questions/877295/python-dict-add-by-valuedict-2
|
|
|
|
|
|
|
|
...including one proposal to treat dicts as `sets of keys
|
|
|
|
<https://mail.python.org/archives/list/python-ideas@python.org/message/YY3KZZGEX6VEFX5QZJ33P7NTTXGPZQ7N/>`_.
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
`Ian Lee's proto-PEP <https://lwn.net/Articles/635444/>`_, and
|
|
|
|
`discussion <https://lwn.net/Articles/635397/>`_ in 2015. Further
|
|
|
|
discussion took place on `Python-Ideas
|
|
|
|
<https://mail.python.org/archives/list/python-ideas@python.org/thread/43OZV3MR4XLFRPCI27I7BB6HVBD25M2E/>`_.
|
2019-10-16 20:41:41 -04:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
(Observant readers will notice that one of the authors of this PEP was
|
|
|
|
more skeptical of the idea in 2015.)
|
2019-10-16 20:41:41 -04:00
|
|
|
|
|
|
|
Adding `a full complement of operators to dicts
|
|
|
|
<https://mail.python.org/archives/list/python-ideas@python.org/thread/EKWMDJKMVOJCOROQVHJFQX7W2L55I5RA/>`_.
|
|
|
|
|
|
|
|
`Discussion on Y-Combinator <https://news.ycombinator.com/item?id=19314646>`_.
|
|
|
|
|
|
|
|
https://treyhunner.com/2016/02/how-to-merge-dictionaries-in-python/
|
|
|
|
|
|
|
|
https://code.tutsplus.com/tutorials/how-to-merge-two-python-dictionaries--cms-26230
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
In direct response to an earlier draft of this PEP, Serhiy Storchaka
|
|
|
|
raised `a ticket in the bug tracker
|
|
|
|
<https://bugs.python.org/issue36431>`_ to replace the
|
2019-10-16 20:41:41 -04:00
|
|
|
``copy(); update()`` idiom with dict unpacking.
|
2019-03-01 11:50:50 -05:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
=========
|
2019-03-01 11:50:50 -05:00
|
|
|
Copyright
|
2020-02-04 18:21:25 -05:00
|
|
|
=========
|
2019-03-01 11:50:50 -05:00
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
This document is placed in the public domain or under the
|
|
|
|
CC0-1.0-Universal license, whichever is more permissive.
|
2019-03-01 11:50:50 -05:00
|
|
|
|
|
|
|
|
2020-02-04 18:21:25 -05:00
|
|
|
..
|
|
|
|
Local Variables:
|
|
|
|
mode: indented-text
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
sentence-end-double-space: t
|
|
|
|
fill-column: 70
|
|
|
|
coding: utf-8
|
|
|
|
End:
|