DEV Community

Waylon Walker
Waylon Walker

Posted on • Updated on • Originally published at waylonwalker.com

Python's Dict Union Operator | Pep 584

Today I was watching the python web conf 2022 and saw @davidbujic use the new Dict Union Operator Live on stage during his Functional Programming talk. This operator was first introduced into python 3.9 with pep584.

Merge Dicts

I've long updated dicts through the use of unpacking. Note that the last item always wins. It makes it pretty easy to make user overrides to default configurations. With pep584 landing in python 3.9 we can now leverage the | operator to achieve the same result.

default_config = {
    'url': 'https://example.com',
    'assets_dir': 'static'
} 
user_config = {'url': 'https://waylonwalker.com'}

# **unpacking goes back much further than 3.9

config = {**default_config, **user_config} 
print(config)
# {'url': 'https://waylonwalker.com', 'assets_dir': 'static'}


# the same can be achieved through the new to python 3.9 | operator

config = default_config | user_config 
print(config)
# {'url': 'https://waylonwalker.com', 'assets_dir': 'static'}
Enter fullscreen mode Exit fullscreen mode


article cover for <br>
 understanding python \*args and \*\*kwargs<br>


understanding python *args and **kwargs



More on unpacking in this post.

Update Dicts

With the release there is also a new update syntax |= that you can use to update. I dont often mutate variables for some reason, so I cant think of a better example for this from my personal use cases. So I will give a similar example to above, except creating a config, then updating it.

# old python <3.9 way
config = {
    'url': 'https://example.com', 
    'assets_dir': 'static' 
} 
config.update({'url': 'https://waylonwalker.com'})

# new python 3.9+ way
config = {
    'url': 'https://example.com',
    'assets_dir': 'static'
}
config |= {'url': 'https://waylonwalker.com'}

print(config)
# {'url': 'https://waylonwalker.com', 'assets_dir': 'static'}
Enter fullscreen mode Exit fullscreen mode

Should you use it?

Are you writing libraries/applications that are only going to be ran on 3.9? Then ya go for it there is nothing to loose. If there is any chance someone is going to run your code on 3.8 or older then just use **, or .update.

RTFM

This is what comes first to my mind on how to use this new syntax, read pep584 for all the gritty details on it.

Links

Top comments (3)

Collapse
 
waylonwalker profile image
Waylon Walker

I'm not selling my users data for dollars per month. If I ever get enough traffic to actually make something more, maybe it will be time to cut a deal with sponsors related to the post.

Collapse
 
ernestvonmoscow profile image
VanPonasenkov

Interesting, haven't coded in Python for a while but interested in the direction in which python is moving

Collapse
 
waylonwalker profile image
Waylon Walker

Some of that might have been my dev.to converter wrapping lines of code... after reading your comment I fixed the code.