The latest changes in Python 3.9
Python has gained in popularity over the past few years, and its use has really exploded in fast-growing areas, like Data Science and Machine Learning. The project is being actively developed to keep pace with all new needs.
Below is a summary of all the new features in Python 3.9.
Python switches to an annual release cycle
Up to this point, Python is developed and released on an 18-month cycle. PEP 602 suggested that the Python development team should adopt the annual release cycle, and that suggestion was accepted.
Annual release cycle means fewer features per release, but also means feature testing will be more responsive, less breakable changes per release. Therefore, there is more incentive for Linux users and distribution managers to upgrade Python more often.
It also means that new features proposed late in the development cycle won't take long to roll into new releases.
The new timeline means Python 3.9 will ship in October 2020. Python 3.10, which officially started pre-alpha development on May 19, 2020, will enter alpha development when Python 3.9 ships and launch October 2021. Future Python releases will follow the same pattern.
Python becomes faster by default
Every revision of Python has improved performance over the previous version. Python 3.9 has two major improvements that increase performance without requiring any changes to existing code.
The first improvement related to more use of the vectorcall protocol was introduced in Python 3.8. vectorcall makes many common function calls faster by minimizing or eliminating the temporary objects made for the call.
In Python 3.9, some of Python's built-in numbers - range, tuple, set, frozenset, list, dict - use internal vectorcall to speed up execution.
The second biggest performance enhancement tool is the more efficient Python source code parser. The new parser for the CPython runtime is not designed to solve performance issues but rather to handle internal inconsistencies in the original parser. Another important benefit, however, is faster parsing, especially for large volumes of code.
New features for Python dictionaries and strings
Python makes it easy to manipulate common data types, and Python 3.9 extends this ease with new features for strings and dictionaries. For strings, there are new ways to remove prefixes and suffixes, operations that have long required a lot of manual manipulation to perform.
For dictionaries, there are now conjugation operators, one to merge two dictionaries into a new dictionary and one to update the contents of one dictionary with another.
The decorator loosened some limitations
Previously, Decorator could only include @ symbol , name (e.g. func ) or period name ( func.method ), and (optionally) a single call ( func.method (arg1, arg2) ) .
With Python 3.9, Decorator can now include any valid expression.
A long-standing way to get around this limitation is to create a function or lambda expression, which will replace a more complex expression when used as a decorator.
Now any expression will work, as long as it yields something that can act as a decorator.
New import operations in Python
In recent versions, Python has extended support for type hinting. This is mainly for the sake of linter (tool used to analyze source and detect code mistakes) and code checkers.
But type hinting is a powerful tool for ensuring consistency across large code bases, so Python code can still benefit from having type hints.
Two new features for type hinting and type annotation were introduced in Python 3.9. Enter a hint for the contents of the collection - for example, lists and dictionaries - that are now available natively in Python.
This means you can for example describe a list in the form of a list [int] - a list of integers - without the typing library needed to do it.
The second addition to Python's input mechanisms is the flexible function and variable annotations. This allows the Annotated style to be used to describe a type of metadata usage, which can be checked ahead of time (with linting tools) or at runtime.
For example, Annotated [int, ctype ("char")] can be used to describe an integer, treated as a char in C. By default, Python will do nothing with such annotation, but It can be used by linter codes.
Internal Python improvements
Internal cleanup, refinement, and modernization Python is an ongoing creative process for developers, and Python 3.9 has made some changes in that direction.
The first is the redesign of the module way. Python extension modules, written in C, can now use a new load mechanism that makes them behave like regular Python modules when imported.
Several modules in the new Python standard library support this behavior: _abc, audioop, _bz2, _codecs, _contextvars, _crypt, _functools, _json, _locale, operator, resource, time, _weakref . The new loading mechanism not only allows extension modules to be handled more flexibly by Python, but also allows for many new capabilities.
The second cleanup initiative is a stable internal ABI for CPython, an ABI that's guaranteed to last for the lifetime of Python 3. Historically, every major revision of Python has been incompatible. ABI with previous versions required the extension modules to be recompiled for every new version.
From now on, any extension modules that use stable ABI will work on Python versions. With Python 3.9, the following modules in the standard library use stable ABI: audioop, ast, grp, _hashlib, pwd, _posixsubprocess, random, select, struct, termios, zlib .
Other changes in Python 3.9
The Python standard library currently supports the IANA Time Zone Database. This database is well maintained and widely used. There is a direct way to use it in Python's datetime library which will save you a lot of time.
New string methods make it easy to remove prefixes and suffixes. This is one of the most common use cases in which everyday use requires too much boilerplate than needed.
The new .removeprefix () and .removesuffix () methods return a modified copy of a string, removing the prefix or suffix in question, as long as they exist in the string.
You should read it
- More than 100 Python exercises have solutions (sample code)
- Bookmark 5 best Python programming learning websites
- For in Python loop
- Manage files and folders in Python
- Multiple choice quiz about Python - Part 3
- 5 choose the best Python IDE for you
- What is Python? Why choose Python?
- Module time in Python
- Python data type: string, number, list, tuple, set and dictionary
- How to install Python on Windows, macOS, Linux
- How to set up Python to program on WSL
- Multiple choice quiz about Python - Part 4