Some of the newest features are incredibly exciting, and it will be amazing to see them used after release. We’ll cover the following ones:
Dictionary Union Operators Type Hinting Two New String Methods New Python Parser
Let’s take a first look at these new features and how to use them. We have the merge operator:
And the update operator |=, which updates the original dictionary:
If our dictionaries share a common key, the key-value pair in the second dictionary will be used:
Dictionary Update with Iterables
Another cool behavior of the |= operator is the ability to update the dictionary with new key-value pairs using an iterable object — like a list or generator:
If we attempt the same with the standard union operator | we will get a TypeError as it will only allow unions between dict types.
Type hinting
Python is dynamically typed, meaning we don’t need to specify datatypes in our code. Since 3.5, we could specify types, but it was pretty cumbersome. This update has truly changed that, let’s use an example: In our add_int function, we clearly want to add the same number to itself (for some mysterious undefined reason). But our editor doesn’t know that, and it is perfectly okay to add two strings together using + — so no warning is given. What we can now do is specify the expected input type as int. Using this, our editor picks up on the problem immediately. We can get pretty specific about the types included too, for example: Type hinting can be used everywhere — and thanks to the new syntax, it now looks much cleaner:
New parser
Python currently uses a predominantly LL(1)-based grammar, which in turn can be parsed by a LL(1) parser — which parses code top-down, left-to-right, with a lookahead of just one token. Now, I have almost no idea of how this works — but I can give you a few of the current issues 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 limitations in the Python syntax (without possible workarounds). This issue highlights that the following code simply cannot be implemented using the current parser (raising a SyntaxError):
LL(1) breaks with left-recursion in the parser. Meaning particular recursive syntax can cause an infinite loop in the parse tree. Guido van Rossum, the creator of Python, explains this here.
All of these factors (and many more that I simply cannot comprehend) have one major impact on Python; they limit the evolution of the language. The new parser, based on PEG, will allow the Python developers to be significantly more flexible — something we will begin to notice from Python 3.10 onwards. This article was originally published on Towards Data Science by James Briggs, an AI Consultant based in London. He is fascinated by the phenomenal advances that are being made within the tech eco-system daily and loves writing about AI, Python, and programming in general. Follow him on Twitter.