5 ms·
The best way to do dictionary union is already symbolic: {**d1, **d2} This provides a clearer symbolic notation for dictionaries analogous to what's alrea
by kbd 7y ago
The best way to do dictionary union is already symbolic:
{**d1, **d2}
This provides a clearer symbolic notation for dictionaries analogous to what's already available with sets. FWIW the pep discusses what this would look like as a method vs an operator:
https://www.python.org/dev/peps/pep-0584/#use-a-method https://www.python.org/dev/peps/pep-0584/#use-a-method
- fulafel 7y agoThe best way for in my view is .union(), the new syntax additions are too cryptic.
- choward 7y agoI agree. Back in the day when I used Ruby, I remember one of the arguments for Python being their belief that there should be one way to do things. Found one reference: > There should be one-- and preferably only one --obvious way to do it. https://www.python.org/dev/peps/pep-0020/ https://www.python.org/dev/peps/pep-0020/
- orf 7y agoThere should be one _obvious_ way to do things. Not _one_ way to do things. The operator is better than the dict unpacking-repacking trick, and will become the obvious way to do it.
- anentropic 7y agowe already have `set1 | set2` for set unions and dict keys are basically a set
- fulafel 7y agoI think that's cryptic too for most people, intuitive just for people who are conversant in bitwise operations.
- anentropic 7y agothese are standard set operations, not bitwise
- fulafel 7y agoThe choice of the "|" operator for set union comes from bitwise operations: bitwise OR works as a union operator if you are using integers as bit vectors to represent sets of boolean attributes. And it was a common idiom back in the day when people used to program in C/assembly, using words as bit vectors was a common way to save memory. Hence "|" as set union is intuitive for people who are familiar with this application of bit vectors.
- heavenlyblue 7y agoI think the OR operator comes from set theory and has nothing to do with the low-level boolean flag fieldsets.
- fulafel 7y agoI looked up set theory a couple of places (WP[1] and Britannica[2]) and didn't find any references of the OR operator in this context.. do you have a link? [1] https://en.wikipedia.org/wiki/Algebra_of_sets https://en.wikipedia.org/wiki/Algebra_of_sets [2] https://www.britannica.com/science/set-theory/Operations-on-sets https://www.britannica.com/science/set-theory/Operations-on-...
- housecarpenter 7y agoThe analogue of the OR operator in set theory is the union operator. People think of them as basically the same thing because of the correspondence between a property and the set of things with that property. If A is the property of being either B or C, then the set of the things that are A is the union of the set of things that are B and the set of things that are C.
- kbd 7y agoNot sure what you're referring to because there is no "union" method/function. There is currently no non-symbolic built-in way to combine dictionaries in an expression. You may be interested to read PEP 584's list of examples of all the real-world code the existence of this operator makes clearer: https://www.python.org/dev/peps/pep-0584/#examples https://www.python.org/dev/peps/pep-0584/#examples