[Python-ideas] PEP: Dict addition and subtraction

Chris Angelico rosuav at gmail.com
Wed Mar 6 18:58:02 EST 2019

On Thu, Mar 7, 2019 at 10:52 AM Josh Rosenberg
<shadowranger+pythonideas at gmail.com> wrote:
> The closest I can come to a thorough definition of what + does in Python (and most languages) right now is that:
> 1. Returns a new thing of the same type (or a shared coerced type for number weirdness)
> 2. That combines the information of the input operands
> 3. Is associative ((a + b) + c produces the same thing as a + (b + c)) (modulo floating point weirdness)
> 4. Is "reversible": Knowing the end result and *one* of the inputs is sufficient to determine the value of the other input; that is, for c = a + b, knowing any two of a, b and c allows you to determine a single unambiguous value for the remaining value (numeric coercion and floating point weirdness make this not 100%, but you can at least know a value equal to other value; e.g. for c = a + b, knowing c is 5.0 and a is 1.0 is sufficient to say that b is equal to 4, even if it's not necessarily an int or float). For numbers, reversal is done with -; for sequences, it's done by slicing c using the length of a or b to "subtract" the elements that came from a/b.
> 5. (Actual addition only) Is commutative (modulo floating point weirdness); a + b == b + a
> 6. (Concatenation only) Is order preserving (really a natural consequence of #4, but a property that people expect)
> Allowing dicts to get involved in + means:
> 1. Fewer consistent rules apply to +;
> 2. The particular idiosyncrasies of Python dict ordering and "which value wins" rules are now tied to +. for concatenation, there is only one set of possible rules AFAICT so every language naturally agrees on behavior, but dict merging obviously has many possible rules that would be unlikely to match the exact rules of any other language except by coincidence). a winning on order and b winning on value is a historical artifact of how Python's dict developed; I doubt any other language would intentionally choose to split responsibility like that if they weren't handcuffed by history.
> Again, there's nothing wrong with making dict merges easier. But it shouldn't be done by (further) abusing +.

Lots of words that basically say: Stuff wouldn't be perfectly pure.

But adding dictionaries is fundamentally *useful*. It is expressive.
It will, in pretty much all situations, do exactly what someone would
expect, based on knowledge of how Python works in other areas. The
semantics for edge cases have to be clearly defined, but they'll only
come into play on rare occasions; most of the time, for instance, we
don't have to worry about identity vs equality in dictionary keys. If
you tell people "adding two dictionaries combines them, with the right
operand winning collisions", it won't matter that this isn't how lists
or floats work; it'll be incredibly useful as it is.

Practicality. Let's have some.


More information about the Python-ideas mailing list