current position:Home>Python 3.10 official release

Python 3.10 official release

2022-01-29 11:21:45 Liu Zhijun

Python The official version was released a few days ago 3.10, Although you may not apply it to the production environment immediately , However, it is recommended to upgrade and experience the following if conditions permit , If you have no conditions, just read my article directly , I've listed a few features that developers may be interested in , See which feature you're most looking forward to .

1、 More friendly error tips

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 code 

For example, if you accidentally miss a closing curly bracket in this code , When running code , In previous versions, syntax errors were directly prompted :

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

Without checking the code carefully, you really can't see at a glance what syntax errors are . And in the python3.10 in , Tips become very friendly and specific , Just tell you "{" Not closing , In this way, the positioning error will be quickly .

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 code 

Similarly , And if you forget to add parentheses in the derivation , If the previous sentence is not correct, it will directly prompt grammatical errors

>>> {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 code 

And now I'll tell you , Did you forget to add 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 code 

Um. , This is humanized .

2、match ... case   Finally here

match ... case Grammar is the function I'm looking forward to , It's not much of an advanced feature , Similar to... In other languages switch ... case grammar , In multi conditional judgment, it is better than if ... elif The code is simpler . It is hard to imagine , This grammar is only now added , Of course , In limine Python The father is unwilling to add this grammatical feature , Fortunately, this grammar finally returned , And changed its name .

I was thinking , Why can't you live with yourself , Unity is called switch ... case Is it not good? ? Maybe this is Python Fascinating place .

Take an example

This is the use of 3.10 Of match case grammar

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 code 

case _   Similar to... In other languages default , When other conditions are not met, execute this line .

With normal if ... else Grammar to write

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 code 

3、 Context manager that supports parentheses

In previous versions , Multiple context managers must be placed on one line or with escape characters “\” Line break

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

#  perhaps 

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

stay 3.10 in , We can use parentheses to put multiple managers on multiple lines , This makes the code look cleaner .

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

4、 New type union operator

In the previous version , For function parameters, if you want to support multiple types , For example, it also supports int and float, Need to use Union:

from typing import Union


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

Now there is a new grammar sugar “|”, It's called the union operator , Can make the code more concise

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

The operator is in the function  isinstance() and  issubclass()  Can also support

# python3.10
>>> isinstance(1, int | str)
True

# python3.7
>>> isinstance(1, (int,float))
True
 Copy code 

Last

When developers ask Python Whether there will be Python4.0 When ,Python My father said frankly not to Python 4.0 Hope . If it is released one day Python4.0, It won't go again 2.x Over to 3.0 Old road . meanwhile , We can't count on Python Of GIL Can remove , It's not that I haven't tried , But get rid of it GIL Then it's slower . If your project is very performance sensitive , Might as well try. pypy,python A branch of .

This article is published on the blog at the same time :foofish.net

Welcome to the official account

https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/216a486abacf4924b743a65f804af491~tplv-k3u1fbpfcp-zoom-1.image

copyright notice
author[Liu Zhijun],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201291121428540.html

Random recommended