Python 3.10 was released a few days ago, and while you won’t necessarily see it in production immediately, it’s recommended that you upgrade if you can. I’ve listed a few features that might be of interest to developers to see which ones you’re looking forward to most.

1. Friendlier error prompts

expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4,
            38: 4, 39: 4, 45: 5, 46: 5, 47: 5, 48: 5, 49: 5, 54: 6,
some_other_code = foo()
Copy the code

For example, if you accidentally leave out a close curly brace, you can run the code with a syntax error in the previous version:

File "example.py", line 3
    some_other_code = foo()
                    ^
SyntaxError: invalid syntax
Copy the code

You really can’t tell where the syntax is wrong without looking at the code. In python3.10, the prompt is very friendly and specific, telling you that “{” is not closed, and locating errors is quick.

File "example.py", line 1
    expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4,
               ^
SyntaxError: '{' was never closed
Copy the code

Similarly, if you forget to add parentheses in a derivation, it will indicate a grammatical error if you don’t agree with a previous statement

>>> {x,y for x,y in zip('abcd', '1234')}
  File "<stdin>", line 1
    {x,y for x,y in zip('abcd', '1234')}
         ^
SyntaxError: invalid syntax
Copy the code

And now I’ll tell you if I forgot to put the parentheses.

>>> {x,y for x,y in zip('abcd', '1234')}
  File "<stdin>", line 1
    {x,y for x,y in zip('abcd', '1234')}
     ^
SyntaxError: did you forget parentheses around the comprehension target?
Copy the code

Well, that’s human.

2, the match… Case has finally arrived

match … Case syntax is one of the features I’m looking forward to, it’s not much of an advanced feature, similar to switch in other languages… Case syntax, when using multiple conditions than if… Elif code is more concise. It’s hard to imagine that this syntax has just been added. Of course, the Python fathers were initially reluctant to add this syntax feature, but the syntax eventually came back under a different name.

I was thinking, why are you against yourself? It’s called switch… Is Case bad? Maybe that’s what’s so fascinating about Python.

Let’s look at an example

This is using the 3.10 Match case syntax

def http_error(status):
    match status:
        case 400:
            return "Bad request"
        case 404:
            return "Not found"
        case 418:
            return "I'm a teapot"
        case _:
            return "Something's wrong with the internet"
Copy the code

Case _ is similar to default in other languages. This line is executed when all else fails.

Use the ordinary if… Else syntax

def http_error(status):
    if status == 400:
        return "Bad request"
    elif status == 404:
        return "Not found"
    elif status == 418:
        return "I'm a teapot"
    else:
        return "Something's wrong with the internet"
Copy the code

3. Context manager that supports parentheses

In previous versions, multiple context managers had to be placed on a single line or wrapped with the escape character “\”

with open("xxx.py", mode="w") as f1, open("yyy.py", mode="w") as f2: Py ", mode="w") as f1, \ open("yyy.py", mode="w") as f2: passCopy the code

In 3.10, we could use parentheses to place multiple managers on multiple lines so that the code looks cleaner.

with (
    open("xxx.py", mode="w") as f1,
    open("yyy.py", mode="w") as f2
):
    pass
Copy the code

4. New type union operators

In previous versions, if you wanted to support multiple types for function arguments, such as both int and float, you used Union:

from typing import Union


def foo(number: Union[ int, float]) -> Union[int, float]:
    return number ** 2
Copy the code

Now there’s a new syntactic sugar “|”, called joint operators, can make the code more concise

def square(number: int | float) -> int | float:
    return number ** 2
Copy the code

This operator is also supported in the isinstance() and issubclass() functions

# python3.10 > > > isinstance (1, int | STR) True # python3.7 > > > isinstance (1, (int, float)) TrueCopy the code

The last

When developers asked if Python would still have Python4.0, the father of Python was blunt about holding out hope for Python4.0. If Python4.0 were ever released, it would not be a repeat of the 2.x transition to 3.0. At the same time, we can’t expect Python’s GIL to be removed. It’s not because we haven’t tried, but because it’s slower. If your project is performance-sensitive, try pYPy, a branch of Python.

This article is also published on foofish.net

Welcome to the public account