Python 3.9 Tutorial: Understanding Python 3.9 Features
Python 3.9 has been yet another advanced version of the Python programming language which was released on October 5, 2020. Python 3.9 is released with significant changes in the terms of programming language and evolution of programming language. The current version of Python is much better and it is upgraded to meet the requirement of modern application development. We have seen the tremendous popularity of Python programming language among the programmers. Python has now become main stream programming language in the field of Data Science and Machine Learning. Data Scientists are using Python programming language for data collection, data cleaning, model development, model training and deployment of machine learning model for global enterprises.
There is huge demand of new features in the Python programming language due to widespread use of this programming language in Machine Learning and Artificial Intelligence projects. Python project development is working hard to develop new features and add improvements in the Python programming language. With the huge effort of the Python project team, Python 3.9 is released with major improvements.
Here are the changes/features of Python 3.9:
1. Python switches to yearly release cycle
Earlier Python used to developed and released in 18 months cycle, but now onward it will be released on the yearly basis. So, every year we will see new version of Python release. The PEP 602 proposed to release the Python on annual basis and the proposal has been accepted by the team. The yearly development and release cycle will ensure fast bug fixing and feature improvements. Python 3.10 is already entered into alpha development phase and it will be released October 2021.
2. Python 3.9 comes with performance improvements
Python 3.9 introduced two big improvements that improve the performance of Python code without making any changes in your existing code. So, if you run your old code on Python 3.9 it will perform much better. So, performance improvements is going to be another good reason of migrating your application to Python 3.9, as you will get performance improvement without any code changes.
Following are two major improvements to Python 3.9 that bring major performance gain:
- Improvements to vectorcall protocol
- Efficient parsing of Python source code
Improvements to vectorcall protocol
The vectorcall protocol was first introduced in Python 3.8 and now in Python 3.9 is comes with more improvements. The vectorcall API is developed to make make many common function calls faster and this is achieved by minimizing or eliminating temporary objects which is usually created to call. There are several built-in in the Python 3.9 such as range, tuple, set, frozenset, list, dict etc. which is using the vectorcall internally. This way code running on Python 3.9 runs faster even without any code modification.
Efficient parsing of Python source code
Python 3.9 introduced Parsing Expression Grammar (PEG) parser which is more stable parser and it has been proposed to replace the the current LL(1)-based parser. The Parsing Expression Grammar (PEG) parser is a a high-performing stable parser.
3. Dictionary Update And Merge Operators
Python 3.9 brings two new operators | and |= to dictionary class. These operators are used for dictionary update and merge operations. The | operator is used for merging dictionaries while the |= operator is used for updating the dictionaries.
Example of Dictionary Update and Merge Operations:
a = {"name":"Deepak Kumar","country":"India"}
b = {"name":"John","country":"USA", "language":"English"}
#merge
c = a|b
>>> print(c)
{'name': 'John', 'country': 'USA', 'language': 'English'}
#update
>>> a |= b
>>>
>>> print(a)
{'name': 'John', 'country': 'USA', 'language': 'English'}
Above example shows you to use new dictionary update and merge functions. Here is the screen shot of the example run on Python 3.9:
4. New String Functions To Remove Prefix and Suffix
5. Type Hinting For Built-in Generic Types
6. Support For IANA timezone In DateTime
7. Ability To Cancel Concurrent Futures
8. AsyncIO and multiprocessing Improvements
9. Consistent Package Import Errors
10. Random Bytes Generation
11. String Replace Function Fix