5 ms·
Updating Dictionary in Python 3.9
- rfrey 7y agoI wanted to read this, but an ad popped up over the screen which I couldn't find a way to dismiss. Even clicking the ad's call to action just opened another window, leaving the full overlay in place. I might remember to try again on desktop where I can kill the element.
- pansa2 7y agoMore generally, see What’s New In Python 3.9 [0]. Plus, within the last few days, PEP 614 (Relaxing Grammar Restrictions On Decorators) [1] was also accepted for Python 3.9. [0] https://docs.python.org/3.9/whatsnew/3.9.html https://docs.python.org/3.9/whatsnew/3.9.html [1] https://www.python.org/dev/peps/pep-0614/ https://www.python.org/dev/peps/pep-0614/
- EamonnMR 7y agoAt a glance Python looks like Algol with nicer syntax but it's stuff like this, listcomps and decorators that let you do some very fun and terse tricks.
- erichurkman 7y agoI tried to read this. I love Python, and am thrilled to see the language evolve. This page has a massive sidebar, another massive sidebar that scrolls with the content, and another sidebar. The content for my screen had about 1" width to read. The various sidebars inexplicable start and stop scrolling as I scrolled down, meaning I got through 1/4 and closed the tab. Atrocious and anti-user.
- dr_zoidberg 7y agoThe examples were good, so it's worse than just "yet another python page filled with ads". But I'll summarize them: * First example, merge with update: # d1 and d2 being dicts d3 = d1.copy() d3.update(**d2) * Second example, using dict(): d3 = dict(d1, **d2) * Third example (3.9 onwards): d3 = d1 | d2 # or d3 = d1.copy() d3 |= d2 The examples also show how each operation works (tl;dr: last value assigned to a key "wins").
- divbzero 7y agoThis is a small but welcome change. Note that the two new operators introduced for dict a | b a |= b brings the behavior in line with the same operators for set. [1] [1]: https://docs.python.org/3.8/library/stdtypes.html#set-types-set-frozenset https://docs.python.org/3.8/library/stdtypes.html#set-types-...
- BerislavLopac 7y agoI have always found dicts and sets being pretty much basically the same thing, with dicts allowing for "attaching" additional values to the set items. In fact, before sets were introduced (in Python 2.3), using dicts (with elements for keys and some arbitrary values -- commonly None -- for values) was a common way to emulate them.
- MichioJP 7y agoGreat
- riffraff 7y agoI am sure this is useful, but I feel python is moving further and further from its zen ("preferably one way") in each version. Also, the ads in this website are terrible.
- BerislavLopac 7y agoThe Zen of Python DOES NOT say "preferably one way" or "only one way", as often misquoted. As can be easily checked by `import this`, the relevant line says: There should be one-- and preferably only one --obvious way to do it. The key word, so often ignored, is OBVIOUS. Now, we can argue what "obvious" means, and what is more obvious to different people. But this one word makes a huge difference.
- deleted 7y ago[deleted]
- riffraff 7y agoI specifically said "preferably" for this reason, and there always will be more than one way to do it, so obvious is implied. This change explicitly creates a situation where you have two equivalent obvious ways to do one thing, dict.update() and dict.|=, and is the latest in a string of such changes. I personally _like_ this change, but python used to be much more conservative.
- divbzero 7y agoFor those annoyed by the ad on this morioh.com blog post, the official Python website offers a succinct description of the same feature: [1] Dictionary Merge & Update Operators Merge (|) and update (|=) operators have been added to the built-in dict class. See PEP 584 [2] for a full description. (Contributed by Brandt Bucher in bpo-36144 [3].) [1]: https://docs.python.org/3.9/whatsnew/3.9.html https://docs.python.org/3.9/whatsnew/3.9.html "What’s New In Python 3.9" [2]: https://www.python.org/dev/peps/pep-0584 https://www.python.org/dev/peps/pep-0584 "PEP 584" [3]: https://bugs.python.org/issue36144 https://bugs.python.org/issue36144 "bpo-36144"
- nrvn 7y agoThere should be one-- and preferably only one --obvious way to do it. “There is not too many ways to initialize and merge dicts in python. Let’s add one more!” Which one is idiomatic now? Imagine a large codebase where all these possible ways of doing one thing are inconsistent and misaligned. I remember myself replacing % string format notation with .format() everywhere. Just for the sake of consistency. And then they introduced one more way to format strings... [0]: https://www.python.org/dev/peps/pep-0584/#motivation https://www.python.org/dev/peps/pep-0584/#motivation