• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Code Specialist

Code Specialist

Code and Programming Blog

  • Home
  • Clean Code
  • Code Principles
  • Code Interview
  • Python

New Syntax Features in Python 3.9

In October 2020, Python 3.9 introduced amazing new syntax features. As of now, you can use the new operators for dictionaries, better type hints and the advanced syntax for decorators. Read more about them in this post!

Home » Python » New Syntax Features in Python 3.9

February 12, 2021 by Jonas Scholl Leave a Comment

Contents hide
1 Merge & Update Operators for Dictionaries
2 Type Hint Improvements
3 Extended Decorator Syntax
3.1 Our Book Recommendations on Python (Affiliate)

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:

  1. Dictionary Merge Operator

    This new operator is the complement for the old way to merge dictionaries, so both operations work the same way:

  2. 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!

Download Source Files

Click to rate this post
[Total: 3 Average: 5]
Jonas Scholl

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.

Our Book Recommendations on Python (Affiliate)

Category iconPython Tag icondecorators,  dictionaries,  Syntax,  type hints

Related Posts

Decorators in Python
5 (1)
February 23, 2021
books and dictionaries containing tricks
Dictionary-Tricks in Python
5 (3)
December 8, 2020
construction worker working on toggle switches
Compensating the Switch-Statement in Python
4.8 (8)
October 25, 2020

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published.

Primary Sidebar

Search

Categories

  • Algorithms
  • Code Interview
  • Code Principles
  • Environment
  • Errors
  • Learn to Code
  • Object Orientation
  • Python
  • Technical Background
  • Write Better Code

Tags

Alternative (1) Announcement (1) Clean Code (6) Code (1) Code Interview Question (2) Code Philosophy (1) Comments (1) Computer Graphics (2) Concurrency (1) Control Characters (1) Data Structures (1) decorators (2) dictionaries (3) Easter Eggs (1) funny (1) Github Actions (1) Marching Cubes (1) Meshing (1) Nice to know (1) Parallelisation (1) Pythonic (3) PyYAML (1) Readability (4) Simple is good (2) Software Engineering (2) switch (1) Syntax (1) Tricks (1) type hints (1) Ubuntu 20.04 (1) Underscores (1) Unix (1) Video (1) Virtualization with Windows (1)

Footer

Recent Posts

  • Decorators in Python
  • New Syntax Features in Python 3.9
  • 5 Ways to Speed Up Python Code
  • Bubble Sort in Python and how to Visualize it
  • Reverse words in a string

Tags

Alternative Announcement Clean Code Code Code Interview Question Code Philosophy Comments Computer Graphics Concurrency Control Characters Data Structures decorators dictionaries Easter Eggs funny Github Actions Marching Cubes Meshing Nice to know Parallelisation Pythonic PyYAML Readability Simple is good Software Engineering switch Syntax Tricks type hints Ubuntu 20.04 Underscores Unix Video Virtualization with Windows

Categories

  • Algorithms
  • Code Interview
  • Code Principles
  • Environment
  • Errors
  • Learn to Code
  • Object Orientation
  • Python
  • Technical Background
  • Write Better Code

Recent Comments

  • Nina on Bubble Sort in Python and how to Visualize it
  • Bhan on Concurrency in Python

Follow Us

  • Facebook
  • Instagram
  • Pinterest
  • Twitter

Quicklinks

  • About us
  • Become a writer

Privacy Policy | Legal Notice | Contact

 

All rights reserved © 2020-2021 code-specialist.com