Merge & Update Operators for Dictionaries
Although it was already relatively easy to merge and update dictionaries, there are now new operators for it. This makes the code much more readable, especially for beginners who aren’t familiar with the techniques you had to use before. If you want to read more about dictionaries and other ways to them, check out our post for Dictionary Tricks in Python!
Python 3.9 introduced two new operators for dictionaries:
- Dictionary Merge Operator
This new operator is the complement for the old way to merge dictionaries, so both operations work the same way:
- Dictionary Update Operator
To update a dict, you can use the new merge operator too. If the dict on the operators right side contains a key that is in the other dict as well, an update will be performed. However, if you want to update a dict in place like the update method does, you can can use another new operator:
In terms of the new dictionary operators we can say that they are only syntactic sugar and don’t introduce new possibilities or logic. However, they make the code more simple and understandable.
Type Hint Improvements
Type Hints are one of the most popular features of Python 3 and make Python development much more comfortable. Nevertheless, specifying collections like lists, tuples or dictionaries was kind of ugly. You could use the builtin types, but using them you couldn’t specify the type of the contents! It is nice to specify a list type, but it is also important what the list contains. To specify the content of collections, you had to import and use the types from the typing module. Aside from the fact that this was annoying, the names of the imported types were in uppercase which sometimes led to confusion.
The good news is: In Python 3.9 you no longer need the typing module! Now you can specify the type hints from the typing module with the builtin types, which is definitely more comfortable.
Extended Decorator Syntax
Decorators are a powerful and smart pattern in Python. There are a few ways to use them, but before Python 3.9 there were also many limitations. When you want to use a decorator for a function, you type the @ symbol followed by the decorator. The cool thing is that instead, you can also write an expression that returns the decorator when it is evaluated. However, before Python 3.9 you could only use functions and the dot syntax to access attributes of objects for this expression.
With the new version of Python, the syntax rules for using decorators allow more operations. To be precise, you can use any expression which is a valid test in an if, elif or while block. This means that you can access list items or dictionary values directly in the decorator syntax for instance.
This change may be of particular interest to those of you who create GUIs in Python. Look at this example where with the logic for buttons when they get clicked:
There are is a workaround for this problem where you create a function with the index number as an argument which returns the list item you want. However, this solution is much more readable and simpler.
What do you think of the syntax changes in Python 3.9? Are you going to use them and what are cool use cases? Share your thoughts in the comments!
There is nothing that excites me more than solving problems and understanding complex concepts. I want to share this enthusiasm by making complex things as simple as possible, so everyone can understand them. Nothing is more frustrating than trying to understand a topic with incomprehensible explanations. That’s why I want to inspire you to become a better developer by simplifying concepts and explaining issues clearly.