Python 3.9: A Peek into the Future of Python
Python 3.9: A Peek into the Future of Python
How much impact Python 3.9 has on the future of Python Programming.
Photo by The Wall Street Journal
Released on Monday 5th October 2020, Python 3.9 brings a lot of exciting new features onboard.
In this article, we will focus on the following :
- Annual Release Cycle for Python
- Interpreter Improvements
- New Built-in features
- New Syntax Features
I honestly do not enjoy reading these articles about how python is dying like Python is Slowly Losing its Charm by Jason Dsouza; even though they are highly opinionated.
The new release of python shows just how much alive it still is and why we should expect much more from this point. Read with me to see how…
Annual Release Cycle for Python
Python 3.9 marks the beginning of a new release calendar reducing the cycle down from 1½ years to only 12 months.
Annual Release Cycle for Python — PEP 602
This will significantly accelerate the release pulse so much so that we may be seeing a new release in October every year.
The change brings with it a handful of advantages, for instance, making the subsequent releases smaller which will consequently encourage sooner bug fixes in the hands of users; just to mention a few.
Read more about PEP 602.
Interpreter Improvements
New PEG parser for CPython
Python 3.9 will adopt a new PEG-based parser in place of the current LL(1). We expect Python 3.9 to be faster as the new PEG parser will present exponential time performance in the worst case as compared to LL(1) parsers. This is true because PEG parsers have an infinite lookahead, this means that they can consider an arbitrary number of tokens before deciding for a rule.
The performance of the new parser has been tuned to come within 10% of the old parser in both speed and memory consumption.
A canonical file test with 100,000 lines endlessly repeating the following three lines was done:
1 + 2 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + ((((((11 * 12 * 13 * 14 * 15 + 16 * 17 + 18 * 19 * 20))))))
2*3 + 4*5*6
12 + (2 * 3 * 4 * 5 + 6 + 7 * 8)
The results for this test file test proved that the new parser is faster and uses less memory than the current parser.
- Parsing and compiling to bytecode takes 1.28 seconds(new).
- Parsing and compiling takes 1.44 seconds(current parser).
We will however not be seeing much action from the new parser until Python 3.10. According to PEP 617, changing to the new parser will follow a well laid-out migration plan.
New Math Functions
The in-built math module has new awesome functions.
GCD — This method can now handle multiple arguments instead of the initial two.
LCM — This new method returns the least common multiple of specified arguments.
Nextafter — The math.nextafter()
function returns the next representable floating-point value following x in the direction of y. If y is less than x, these functions will return the largest representable number less than x.
ULP — It stands for “Unit in the Last Place”. The function returns the value of the least significant bit of the float x.
Usage
>>> import math
>>> # gcd
>>> math.gcd(36,72,24)
12
>>> #lcm
>>> math.lcm(36,72,24)
72
New Built-in Features
removeprefix() and removesuffix() string methods
Theremoveprefix('prefix')
and removesuffix('suffix)
are string methods that can be used to easily remove an unwanted prefix or suffix respectively.
Usage
>>> text = 'Python 3.9'
>>> text
'Python 3.9'
>>> text.removeprefix('Python ')
'3.9'
>>> text.removesuffix(' 3.9')
'Python'
There may be confusion between the lstrip/rstrtip
and the new remove(prefix/suffix)
, for that reason, the following differences should be considered.
lstrip/rstrip:
- The argument is interpreted as a character set.
- The characters are repeatedly removed from the appropriate end of the string.
remove(prefix/suffix):
- The argument is interpreted as an unbroken substring.
- Only at most one copy of the prefix/suffix is removed.
New Syntax Features
Dictionary Union
Previously we only had the dict.update
and {**d1, **d2}
methods of merging dictionaries. Now we have two more operators for achieving the union.
Dictionary Merge **|**
>>> old = {'one':1, 'two':2, 'three':3}
>>> new = {'four':4, 'five':5}
>>> old | new
{'one':1, 'two':2, 'three':3, 'four':4, 'five':5}
Update Operators **|=**
>>> old = {'one':1.0, 'two':2.0, 'three':3}
>>> new = {'one':1.0, 'two':2.0, 'three':3.0, 'four':4.0}
>>> old |= new
{'one':1.0, 'two':2.0, 'three':3.0, 'four':4.0}
Update with Iterables
Another really cool feature of the dictionary update operator, |=
is that it can use iterables to update a dictionary.
>>> even = {'two':2, 'four':4}
>>> other = [('six', 6), ('eight', 8)]
>>> even |= other
{'two':2, 'four':4, 'six':6, 'eight':8}
Conclusion
Every new python release comes with features that make it faster and more powerful. Python 3.9 will undoubtedly leave a very big impact on the subsequent releases beginning with Python 3.10 set for October 2021.
With the python community growing immensely, I believe that python programming has never been more alive.
Get the download to enjoy the new features.
You can reach out to me for questions or suggestions on Twitter.
Thanks for reading! Give the original article a clap on Medium.