Live data from Hacker News

Dictionary union (PEP 584) is merged

github.com

71–80 of 87 posts

Re: Dictionary union (PEP 584) is merged

#71

It seems that just using + as the operator was reject because it's: "Too specialised to be used as the default behavior." What does that mean? It works for lists, obviously lists don't need to worry about duplicated values, but it's kind non-intuitive that + won't work for dicts. It think many people view dicts and lists as the same general type of data structure.

>> It think many people view dicts and lists as the same general type of data structure.

Is that a joke or are you from PHP world?

Re: Dictionary union (PEP 584) is merged

#72
post #63

Earlier quoted context omitted.

these are standard set operations, not bitwise

The 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 applic…

I think the OR operator comes from set theory and has nothing to do with the low-level boolean flag fieldsets.

Re: Dictionary union (PEP 584) is merged

#74
post #63

Earlier quoted context omitted.

The 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 applic…

I think the OR operator comes from set theory and has nothing to do with the low-level boolean flag fieldsets.

I 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

[2] https://www.britannica.com/science/set-theory/Operations-on-...

Re: Dictionary union (PEP 584) is merged

#76
post #74

Earlier quoted context omitted.

I think the OR operator comes from set theory and has nothing to do with the low-level boolean flag fieldsets.

I 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 [2] https://www.britannica.com/science/set-theory/Operations-on-...

The 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.

Re: Dictionary union (PEP 584) is merged

#77
post #45

Earlier quoted context omitted.

That ship had long sailed with string formatting anyways.

F-strings are the one obvious way to do string formatting. There may be other ways, for legacy backwards compatibility reasons, but f-strings are the way to do string formatting.

Yes, unless a template is needed later. That's why the others continue to exist.

Re: Dictionary union (PEP 584) is merged

#78
post #30
post #16

Fantastic. Especially glad they went with | over +, that’s always felt like the natural way I’ve wanted to do this. Looking forward to more set-like operators in the future!

Thank goodness sanity prevailed on the operator! We had a whole discussion on HN last time[1] about this, where I argued that dicts are logically subclasses of sets and therefore should share operators. When I saw this headline I accepted my fate of typing the "wrong" operator from now on and liking Python just a tiny bit less for the inconsistency. So glad they reconsidered. [1] https://news.ycombinator.com/item?id=…

Guido stated his preference to | and the pep was changed.

Re: Dictionary union (PEP 584) is merged

#80
post #40
post #37

Earlier quoted context omitted.

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

The best way for in my view is .union(), the new syntax additions are too cryptic.

Not 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

Post reply on HN