4 ms·
+ is not a supported operand for dict. You can probably subclass dict and add the plus operand
by Kpourdeilami 9y ago
+ is not a supported operand for dict. You can probably subclass dict and add the plus operand
- mixmastamyk 9y agoWhy add that crazy syntax when a + would be a lot clearer?
- akvadrako 9y agoIt makes sense in context. {} is a dict literal, and * * dict is already used other places to expand a dict into key-value pairs. Given a = {1: 1} b = {2: 2} This c = {**a, **b} is equivalent to c = {1: 1, 2: 2}
- namelost 9y agoa|b would be the obvious choice, because it already works for set: {1,2}|{2,3} == {1,2,3} I guess the reason they went for the weird {∗∗a, ∗∗b} syntax is that it is not a commutative operation.
- mixmastamyk 9y agoWouldn't mind that either.
- akvadrako 9y agoI guess I didn't explain it clearly enough. ∗∗a is not new syntax, it's just applicable in a new context.
- ericfrederich 9y agoCorrect. This is called orthogonality. I just used it now to discover that this same syntax works for both sets and lists (using * instead of ) >>> s1 = {'a', 'b', 'c'} >>> s2 = {1, 2, 3} >>> {*s1, *s2} {'b', 1, 2, 3, 'c', 'a'} >>> l1 = ['a', 'b', 'c'] >>> l2 = [1, 2, 3] >>> [*l1, *l2] ['a', 'b', 'c', 1, 2, 3]