By Varun Singh Translated by Ouyang Jin proofread by Wang Khan This article is about 1700 words. It is recommended to read for 5 minutes. This article introduces the new features of the new version of Python.

Source: Canva - edited by Varun Singh
We all know that Python has been popular for a long time. As a python developer, I am happy to share with you the continuous updates and improvements of Python in each new version.
The latest version of Python 3.10 has some major improvements. Next, I will introduce the five most significant updates released with this new version of Python.
1. Optimize error messages
It's important here. For Python developers, when you encounter an error when coding, the error message can help you find the problem in the code. Compared with previous Python versions, the improved error message can make this process easier.
For example, the following code has no parentheses at the end of the second line:
# I am coding in Python and this is first line ;) my_list = ["Hello", "Python!" print(my_list)
In the previous version - Python 3 9 and earlier versions, you will see the following error reports——
File "my_precious.py", line 3 print(my_list) ^ SyntaxError: invalid syntax
Well, invalid syntax! Now, as a developer, what can you understand from this error message? Personally, I don't have any information other than adding a wrong syntax somewhere on line 3.
But does the error really appear on line 3?
In this case, Python 3.10 became my savior with its latest update. For the same piece of code, Python 3.10 will throw the following error message——
File "my_precious.py", line 2 news = ["Hello", "Python!" ^ SyntaxError: '[' was never closed
Oh, roar! The line number and very specific error information let me find and fix the error directly and continue programming!
Here's another example I'm trying to see if the error message is clear enough——
# missing_comma.py dc_characters = { 1: "Superman" # Comma missing 2: "Batman", 3: "Joker" } .... Output: File "dc_characters.py", line 4 10: "October" ^^^^^^^^^ SyntaxError: invalid syntax. Perhaps you forgot a comma?
This is really a cool update in Python version 3.10. You can leave a message in the comment area to share your ideas.
2. Simpler type union syntax
The typing module is used to add static types to python. In past Python versions, more tools have been converted from typing to built-in functionality to avoid importing static types every time.
Now let's see what has changed here——
# Before Python 3.10 Release from typing import Union def f(list: List[Union[int, str]], param: Optional[int]): pass # In Python 3.10 Release def f(list: List[int | str], param: int | None): pass # Calling the function f([1, "abc"], None)
In Python 3.10, you can now use the pipe operator (|) to specify a collection of types instead of importing Union from the typing module.
In addition, the existing typing The syntax of Union and | should be equivalent, as shown in the following comparison——
int | str == typing.Union[int, str] typing.Union[int, int] == int int | int == int
2. Use multiple 'with' statements in multiple lines
Python does support multiline statements by using backslashes (\), but some structures in Python should not use backslashes to write multiline statements. One of them is a context manager with multiple lines of with() statements. For example——
# Before Python 3.10 Release with (open("a_really_long_foo") as foo, open("a_really_long_bar") as bar): pass Traceback (most recent call last): File "<input>", line 1, in <module> File "demo.py", line 19 with (open("a_really_long_foo") as foo, ^ SyntaxError: invalid syntax
Yes, it doesn't look like a function, but it is a significant improvement over previous versions, because you may have encountered use cases using multi line context manager, but it can't be executed due to the above error.
If not, let's give more examples of what you can do with the context manager in Python version 3.10——
# After Python 3.10 Release from contextlib import contextmanager @contextmanager def f(x): try: yield x finally: pass # Example 1 with f('c') as a, f('a') as b: pass # Example 2 with f('c') as a, f('a') as b, f('a') as c: pass
Isn't it great that you can now have multiple lines of context manager statements without using backslashes?
3. Better type alias
Type aliases allow you to quickly define new aliases that can be created for complex type declarations. For example——
# Before Python 3.10 UserInfo = tuple[str, int]
This is usually feasible. However, it is often impossible for the type checker to know whether such a statement is a type alias or just a definition of a regular global variable.
# In Python 3.10 from typing import TypeAlias Card: TypeAlias = tuple[str, str] Deck: TypeAlias = list[Card]
The python code above declares an alias UserInfo for tuple[str, str] because it is a data type that combines multiple types of values. In the example, it is a string and an integer. In addition, adding a TypeAlias annotation can clarify the intent to the type checker and anyone reading the code.
4. Stricter sequence compression
zip() is a built-in function in Python. You may have used it when combining multiple lists / sequences. Python3.10 introduces a new strict parameter, which adds a run-time test to check whether all compressed sequences have the same length.
For example——
# Before Python 3.10 names = ["Tom", "Harry", "Jessica", "Robert", "Kevin"] numbers = ["21024", "75978", "92176", "75192", "34323"]
zip() can be used to iterate over the three lists in parallel:
list(zip(names, numbers)) ..... Output: [(Tom, 21024), (Harry, 75978), (Jessica, 92176), (Robert, 75192), (Kevin, 34323)]
Let's use the names and numbers of the two sequences shown above again. Now, the only difference between these sequences is that the length of the number is different from the sequence name, as shown below——
# Before Python 3.10 names = ["Tom", "Harry", "Jessica", "Robert"] # Kevin is missing numbers = ["21024", "75978", "92176", "75192", "34323"] # Zipping using zip() list(zip(names, numbers)) ...... Output [(Tom, 21024), (Harry, 75978), (Jessica, 92176), (Robert, 75192)]
Notice that all the information about the name Kevin has disappeared!
If your data set is large, such errors are difficult to find. Moreover, even if you realize that there is a problem, it may not be easy to solve.
Assuming that the compressed sequence has the same length can help you avoid this difference.
In Python 3.10, the strict parameter can help you avoid this from the beginning——
# In Python 3.10 names = ["Tom", "Harry", "Jessica", "Robert"] # Kevin is missing numbers = ["21024", "75978", "92176", "75192", "34323"]# Zipping using zip() with additional Parameter strict=True list(zip(names, numbers, strict=True)).... Output: Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: zip() argument 2 is shorter than argument 1
Although strict doesn't really add any new features to zip(), it can help you avoid hard to find errors.
Additional updates in Python 3.10
There are many bug fixes and other minor updates in this version. If you are interested, you can find them on the official release page.
(https://www.python.org/downloads/release/python-3100/)
Original title:
Python 3.10 Released — Top 5 New Features You should Know
Original link:
https://varun-singh-01.medium.com/python-3-10-released-top-5-new-features-you-should-know-bf968ac99230
Editor: Yu tengkai
Introduction to the translator
Ouyang Jin, a master student at Eindhoven University of technology. I like data science and artificial intelligence. Welcome the exchange and collision of different views and ideas, be curious about the unknown and stick to your love.