New Features in Python 3.9

Browse on the best features included in the latest Python iteration.


The time has come, the release of a new version of Python is inevitable. It is now in beta (3.9.0b3), but soon we will see the full version of Python 3.9.

Some of the latest features are incredibly interesting, and it will be amazing to see their use after release. We will consider the following:

  • Dictionary Combination Operators
  • Type hinting
  • Two new string methods
  • New Python Parser – It is very cool

Let’s first look at the new features and how we will use them.

Dictionary Combination

One of the new and already my favorite features with syntax. If we have two dictionaries a and bthat we need to combine, we now use the union operators.

We have a merge operator “|”:

a = {1: 'a', 2: 'b', 3: 'c'}
b = {4: 'd', 5: 'e'}
c = a | b
[Out]: {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}

And the update statement “|=”, Which updates the source dictionary:

a = {1: 'a', 2: 'b', 3: 'c'}
b = {4: 'd', 5: 'e'}
a |= b
[Out]: {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}

If our dictionaries have shared key, a key-value pair from the second dictionary will be used:

a = {1: 'a', 2: 'b', 3: 'c', 6: 'in both'}
b = {4: 'd', 5: 'e', 6: 'but different'}
print(a | b)
[Out]: {1: 'a', 2: 'b', 3: 'c', 6: 'but different', 4: 'd', 5: 'e'}

Dictionary update using iterations

Another interesting behavior of the “|=”- the ability to update the dictionary with new key-value pairs using an iterative object – for example, a list or generator:

a = {'a': 'one', 'b': 'two'}
b = ((i, i**2) for i in range(3))
a |= b
[Out]: {'a': 'one', 'b': 'two', 0: 0, 1: 1, 2: 4}

If we try to repeat the same with the standard union operator “|” we’ll get TypeErrorsince it will only allow joins between types dict.

Type hinting

Python is dynamically typed, that is, we do not need to specify data types in our code.
This is fine, but sometimes it can be confusing, and suddenly Python’s flexibility becomes more unpleasant than anything else.

Starting with version 3.5, we could specify types, but it was rather cumbersome. The current update has really changed the approach, let’s see an example:

In our function add_int we clearly want to add two identical numbers (for some mysterious vague reason). But our editor does not know this, and it’s perfectly normal to join two lines using “+”, So we will not see any warning.

Now we can specify the expected type as int. Using this, our editor immediately detects the problem.

We can also get information about expected types, for example:

Type hinting can be used everywhere – and thanks to the new syntax it now looks much cleaner:

String Methods

Not as spectacular as the other new features, but still worth mentioning, as this can be useful. Two new string methods have been added for removing prefixes and suffixes:

"Hello world".removeprefix("He")
[Out]: "llo world"
Hello world".removesuffix("ld")
[Out]: "Hello wor"

New parser

This is more of a hidden change, but it may be one of the most significant changes for the future development of Python.

Python currently uses a grammar based primarily on LL (1), which in turn can be parsed by the LL (1) parser, which parses the code from top to bottom, from left to right, with the ability to view only one token.

I have almost no idea how this works, but I can tell you about several actual problems in Python due to the use of this method:

  • Python contains non-LL (1) grammar; because of this, some parts of the current grammar use workarounds, creating unnecessary complexity.
  • LL (1) creates restrictions in Python syntax (without possible workarounds). This problem emphasizes that the following code simply cannot be implemented using the current parser (throws a SyntaxError error):
    with (open("a_really_long_foo") as foo,
          open("a_really_long_bar") as bar):
  • LL (1) breaks with left-recursion in the parser. That is, a particular recursive syntax can cause an endless loop in the parsing tree, Guido van Rossum, creator of Python, explains it here.

All of these factors (and many others that I just can’t understand) have one big impact on Python; they limit the development of the language.

New parser based on Peg, will give Python developers much more flexibility – and we’ll start to notice this starting with Python 3.10.

This is what we will see in future Python 3.9. If you’re impatient, the most recent beta is 3.9.0b3 – available here.

Learn the details of how to get a sought-after profession from scratch or Level Up in skills and salary by completing SkillFactory paid online courses:

Read more

  • The coolest Data Scientist does not waste time on statistics
  • How to Become a Data Scientist Without Online Courses
  • Sorting cheat sheet for Data Science
  • Data Science for the Humanities: What is Data
  • Steroid Data Scenario: Introducing Decision Intelligence

Similar Posts

Leave a Reply Cancel reply