3

Python 3.10,来了!这些新功能你知道吗

 2 years ago
source link: https://zhuanlan.zhihu.com/p/422449833
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Python 3.10,来了!这些新功能你知道吗

来源:Python大数据分析
作者:费弗里

1 简介

就在几天前,Python3.10的第一个正式版本3.10.0发布,之前我们只是从其各个测试版本中捕风捉影地知晓了一些可能加入的新特性,而在正式版本中,我们得以一睹其正式加入的诸多新特性。

v2-61533cf9548f94e7969c8ce57eca3597_720w.jpg

本文我就将带大家学习如何通过conda快速安装3.10正式版本的Python,并体验其重要的一些新特性。

2 Python 3.10正式版本重要特性一览

如果你已经安装了conda,那么直接通过conda-forgechannel,只需要下面这一行命令即可创建新的Python3.10正式版本虚拟环境:

conda create -n py310 python=3.10 -c conda-forge -y

完成安装之后,激活环境进入Python的shell,可以看到版本为3.10.0,离谱的是我在使用pycharm设置环境后,崭新的3.10.0环境竟然被识别为3.1(真是醉了) ,于是我换成用vscode进行演示:

v2-8207b8fc065e2d4fea9926e8b12bc3fe_720w.jpgv2-922b1b1f7de98fee6154f3174d13d857_720w.jpg

下面就让我们来体验其重要的一些新特性吧~

2.1 新的结构模式匹配语法

上一次为Python引入新的语法和关键字还是async,而在这次的新版本中,引入了新的关键字matchcase,从而帮助我们得以在Python中使用到其他语言中类似switch的语法,使用方式如下面例子所示,对于match其后声明的值,后续的每个case会验证是否与其对应值相等,最后的case _中的_代表Irrefutable Pattern,相当于通配符,但是只允许置于「最后」一个case中:

import sys

match sys.argv[1]:
    case '1':
        print(1)
    case '2':
        print(2)
    case '3':
        print(3)
    case _:
        print('其他输入')

match-case语法配合上通配符_,当输入容器类型的数据结构时,可以进行一些更加自由的判断:

import sys

# 1 新的结构模式匹配语法
match sys.argv[1:]:
    case ['1', '2', '3']:
        print('分支1')
    case ['4', _, '6']:
        print('分支2')
    case ['7', '8', _]:
        print('分支3')
    case _:
        print('超出已知选项!')

更有趣的是,我们还可以使用任意自定义变量名,在match-case的作用域内辅助通配匹配,从而实现类似下面例子的效果:

import sys

# 1 新的结构模式匹配语法
match (int(sys.argv[1]), int(sys.argv[2])):
    case (0, 0):
      print("原点")
    case (0, y):
      print(f"Y={y}")
    case (x, 0):
      print(f"X={x}")
    case (x, y):
      print(f"X={x}, Y={y}")
    case _:
      print('输入非法!')

类似的,针对对象的属性值,也可以进行类似的通配判断,更多用法你可以参考下面的例子进行拓展:

import sys

class Demo:
    x: int
    y: int

demo = Demo()
demo.x = int(sys.argv[1])
demo.y = int(sys.argv[2])

# 1 新的结构模式匹配语法
match demo:
    case Demo(x=0, y=0):
      print("原点")
    case Demo(x=0, y=y):
      print(f"Y={y}")
    case Demo(x=x, y=0):
      print(f"X={x}")
    case Demo(x=x, y=y):
      print(f"X={x}, Y={y}")
    case _:
      print('输入非法!')

2.2 更清晰的错误提示

Python3.10中,针对常见的各种错误类型,执行代码后的错误提示更加明确,譬如下面是3.9与3.10针对同一种错误的错误输出信息对比:

2.3 支持括号包裹的多上下文管理器

这个特性其实在3.9中就开始出现,在3.10中进一步得到完善,使得下面的用法合法(与3.7进行对比):

2.4 更方便的联合类型提示设置

在之前的版本中,当我们用到多类型提示时,得这样写:

from typing import Union, string

def some_funcion(flexible_parameter: Union[int, string]) -> Union[int, string]:
    return flexible_parameter

而在3.10中,多类型联合不再必须用到Union,使用|连接即可,非常的方便:

from typing import string

def some_funcion(flexible_parameter: int | string) -> int | string:
    return flexible_parameter

2.5 更加准确的错误代码行位置提示

Python3.10中,代码的错误提示,对于具体错误行位置的提示更加准确了:

v2-cb51ef135473c29021e8aafb068ebb67_720w.jpg

以上就是本文的全部内容,欢迎在评论区与我进行讨论。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK