Python 3.10正式发布,你尝鲜了吗?
本文参考自 Python官方文档 : Python Release Python 3.10.0 | Python.org
在正值国庆假期人山人海的2021年10月4号,Python官方正式发布了Python3.10.0 。作为一只假期期间宅着不动的coding人,自然是第一时间体验了一波。相较于之前的版本,该版本有以下主要变更。
新的 Union Type表达 新版本简化了 Union Type 的使用 ,改为更为简洁的|
旧版:
1 2 from typing import Union a: Union [int , str ] = 1
新的版本:
二者完全等价:
1 Union [int , str ] == int | str
这类变化在其他地方也相似:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 def f (list : List [int | str ], param: int | None ) -> float | str : pass f([1 , "abc" ], None ) typing.List [str | int ] list [str | int ]typing.Dict [str , int | float ] dict [str , int | float ]
该特性也可用于 isinstance
和issubclass
1 2 3 4 5 isinstance ("FunnySaltyFish" , int |str )issubclass (str , str |int )
zip 可选严格模式 zip新增可选参数strict
, 当该选项为True时,传入zip的两个可迭代项长度必须相等,否则将抛出 ValueError
旧版(及不加此参数),当二者长度不等时,以长度较小的为准
1 2 3 4 5 6 7 8 9 names = ["a" ,"b" ,"c" ,"d" ] numbers = [1 ,2 ,3 ] z = zip (names,numbers) for each in z: print (each)
设置strict为True
1 2 3 4 5 6 7 8 9 10 11 z = zip (names,numbers,strict=True ) d:\projects\python\learn\Py310探索.py in <module> 3 numbers = [1 ,2 ,3 ] 4 z = zip (names,numbers,strict=True ) ----> 5 for each in z: 6 print (each) ValueError: zip () argument 2 is shorter than argument 1
带括号的上下文管理器 with可以加括号了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 with (CtxManager() as example): ... with ( CtxManager1(), CtxManager2() ): ... with (CtxManager1() as example, CtxManager2()): ... with (CtxManager1(), CtxManager2() as example): ... with ( CtxManager1() as example1, CtxManager2() as example2 ): ...
如
1 2 3 4 5 6 7 8 9 10 11 12 import pathlibp = pathlib.Path() p1 = p/"text1.txt" p2 = p/"text2.txt" with ( p1.open (encoding="utf-8" ) as f1, p2.open (encoding="utf-8" ) as f2 ): print (f1.read(), f2.read(), sep="\n" )
显式类型别名 使用 TypeAlias 显式标注类型别名,提高可读性
旧的方式:
1 2 3 x = int def plus_int (a:x,b:x ) -> x: return a+b
可以看到,x很容易被搞混
新的方式:使用 TypeAlias表明这是个别名
1 2 3 4 from typing import TypeAliasx : TypeAlias = int def plus_int (a:x,b:x ) -> x: return a+b
match…case语句 对,就是其他语言的switch-case
,python终于提供了支持,还是加强版的
完整语法参见:PEP 634 – Structural Pattern Matching: Specification | Python.org
举几个例子:
基本的类型匹配:
1 2 3 4 5 6 7 8 day = 6 match day: case 1 : print ("星期一" ) case 6 | 7 : print ("周末" ) case _ : print ("其他情况" )
subject:这在处理命令行参数的时候特别有用
1 2 3 4 5 6 7 8 9 10 11 12 13 """ @copyright : [FunnySaltyFish](https://funnysaltyfish.github.io) @date : 2021/10/05 21:08:42 """ command = "save 1.txt" match command.split(" " ): case ["list" ]: print ("列出文件~" ) case ["save" , file_name]: print (f"保存文件到 {file_name} " ) case ["copy" ,source,target]: print (f"拷贝 {source} -> {target} " )
也可以匹配对象:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class Person (): pass class Student (Person ): def __init__ (self, id : int ) -> None : self.id = id class Teacher (Person ): def __init__ (self, name: str ) -> None : self.name = name a = Student(1 ) match a: case Student(id = 2 ): print (f"这是位学生,且id正好是2" ) case Student(): print (f"这是学生,id为{a.id } " ) case Teacher(): print (f"这是老师, 姓名为{a.name} " )
当然也可以匹配字典:
1 2 3 4 5 6 7 8 9 10 11 d = { "name" : "李四" , "age" : 18 , "hobby" : "阅读" } match d: case {"name" :"张三" , **args}: print ("这是张三" , args) case {"name" : name , "age" : age, "hobby" : hobby}: print (f"我叫{name} , 今年{age} 岁, 喜欢{hobby} " )
更复杂的还有结合Guard、匹配捕获等使用,具体可以参见:PEP 635 – Structural Pattern Matching: Motivation and Rationale | Python.org 和 PEP 636 – Structural Pattern Matching: Tutorial | Python.org
更友好的报错提示 现在,当你的括号、引号未闭合时,python会抛出更加清晰明了的错误
1 2 3 4 5 6 str = "未闭合的str File " d:\projects\python\learn\Py310探索.py", line 90 str = " 未闭合的str ^ SyntaxError: unterminated string literal (detected at line 90 )
1 2 3 4 5 6 arr = [1 , 2 , 2 , 3 File "d:\projects\python\learn\Py310探索.py" , line 91 arr = [1 , 2 , 2 , 3 ^ SyntaxError: '[' was never closed
其他一些更新: distutils 被弃用 推荐使用 setuptools
需要 OpenSSL 1.1.1 及以上版本 移除 Py_UNICODE编码API PyUnicodeObject的wstr被弃用,并将在之后移除 完。摸鱼去了。
作者:FunnySaltyFish 主页:https://funnysaltyfish.github.io/