
Updating Dictionary in Python 3.9 - nasa8x
https://morioh.com/p/825e09f809d0?f=5c22e47440738156a7078a19
======
erichurkman
I 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
The 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").

------
rfrey
I 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.

~~~
Operyl
I think I was able to instinctively touch the background when it popped up for
me (I tend to do these things without thought now). Seemed to work on iOS.

------
divbzero
For 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"

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

~~~
riffraff
I 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.

------
pansa2
More 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/)

------
nrvn
There 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)

------
divbzero
This 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-...](https://docs.python.org/3.8/library/stdtypes.html#set-types-set-
frozenset)

~~~
BerislavLopac
I 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.

------
EamonnMR
At 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.

------
MichioJP
Great

