Why is Python 3.10 good?
Today, this article is going to be devoted to the list of updates and new features of the latest release of Python, namely version 3.10. Yes, you can say that I am a little bit late with this article as the release of Python 3.11 is already approaching. However, Python 3.11 has not been released […]
Technologies
Today, this article is going to be devoted to the list of updates and new features of the latest release of Python, namely version 3.10. Yes, you can say that I am a little bit late with this article as the release of Python 3.11 is already approaching. However, Python 3.11 has not been released yet, and not even all clouds VPS services offer servers with Ubuntu 22.04 including the Python version 3.10.n. In the next couple of years, all large–and not so large–projects will be cautiously switching to Python 3.10.
Updates and New Features
I want to describe these updates and new features briefly in this article, as well as show you some examples of their practical applications.
First, I want to share the background of writing this article. Recently, one of our regular clients decided to order a Python project from us. Although to be more precise, he didn’t order a new project but the transfer of one of his old active Python projects. It was a relatively small web application, which “hung” on the old Ubuntu version 14 with Python 3.5.
This project was assigned to my newly created team, and we had to update Python to the latest version (at the time of this writing it was version 3.10), update Django to the latest version (4.0.4), and also update to the latest available versions of all used libraries, dependencies, as well as a number of fixes and improvements in the Front-End.
I will skip the stage of launching the project on the local machine, optimizing for Python 3.10 some parts of the coding, and immediately proceed to the stage of studying the documentation of the latest release. At the same time, I am going to tell you what the differences between Python 3.10 and its previous versions exactly are.
Easier Code Writing
So, let’s take a look at the points when this version can make writing code easier for us Back-End developers and possibly not just for us:
- It is worth noting that the security improvement in the terms of unpacking EXE files, that everything compiled with Python 3.10 can no longer be unpacked back to get the source code.
2. I would like to note that in this version of Python we managed to achieve an almost 30% increase in performance of the following methods:
- “str” (from 82 to 60 ns)
- “bytes” (from 85 to 60 ns)
- “bytearray” (from 93 to 73 ns)
Yes, the acceleration of only 25-30 ns is quite small, but if we calculate the total number of using these methods in all libraries of your Python project, we will get a good result.
3. Now you can open several context managers and even different types at the same time, for example:
with(
open(‘text_1.txt’, ‘r’, encoding=’utf-8′) as the first_file,
open(‘text_2.txt’, ‘r’, encoding=’utf-8′) as the second_file,
):
4. An improvement has also been made in Python Annotations (Type-Hints).
Instead of the old version of the annotation description:
from typing import Union
def my_func(number: Union[int, float]) -> Union[int, float]
We will write like this:
def my_func(number: int | float) -> int | float
As you can see, instead of Union, the symbol “|” is used, and now there is no need to import it from “typing.” The situation is the same with “Optional.”
The same example can be used in expressions or methods, for example:
isinstance(aaa, int | float | str )
5. Creation of type aliases has become more visual due to the use of the new “TypeAlias” property.
6. A new method “bit_count” has been added to count the number of bits. For-example:
aaa = 2345
aaa.bit_count()
7. Added “kw_only=True” argument to @dataclass decorator
@dataclass(kw_only=True)
8. Newbies that are just learning Python have not been forgotten, so the type hints have been improved. For example, hints when creating lists:
“x = [1, 2 3, 4]”
instead of the message “invalid syntax” we get “invalid syntax. Perhaps you forgot a comma?”
Or, for example, the message about the forgotten closing bracket:
Print (“Hello World” is a SyntaxError and there will be written that exactly the parenthesis “(” is not closed.
9. Pattern matching – this is generally a very cool feature, but it is too voluminous to describe in this article. That is why I will write about Pattern matching with examples in my next article.
Conclusion:
Considering all of the above, I can say that my switch to Python 3.10 was the best decision after switching to Python 3.7.
So, if you just want to create a Python project, order a Python project, or just update your old projects, you can safely choose Python 3.10. This will allow you not to think about upgrading Python for at least the next 2-3 years.
Special thanks to those who have read the article up to this point 🙂 May the force be with you!
If you need a Python project, custom software, cybersecurity, or quality assurance, schedule a free assessment with Swan Software Solutions to find out how our team can help your team.