来源:juejin。cnpost7015590447745613854 Python3。10正式发布,你尝鲜了吗? 本文参考自Python官方文档:PythonReleasePython3。10。0Python。org〔1〕 在正值国庆假期人山人海的2021年10月4号,Python官方正式发布了Python3。10。0〔2〕。作为一只假期期间宅着不动的coding人,自然是第一时间体验了一波。相较于之前的版本,该版本有以下主要变更。 新的UnionType表达 新版本简化了UnionType的使用,改为更为简洁的 旧版:fromtypingimportUnion a:Union〔int,str〕1 新的版本:a:strint1 二者完全等价:Union〔int,str〕intstrTrue 这类变化在其他地方也相似:旧版: deff(list:List〔Union〔int,str〕〕,param:Optional〔int〕)Union〔float,str〕 deff(list:List〔intstr〕,param:intNone)floatstr: pass f(〔1,abc〕,None) 旧版: typing。List〔typing。Union〔str,int〕〕 typing。List〔strint〕 list〔strint〕 旧版: typing。Dict〔str,typing。Union〔int,float〕〕 typing。Dict〔str,intfloat〕 dict〔str,intfloat〕 该特性也可用于isinstance和issubclassTrue isinstance(FunnySaltyFish,intstr) True issubclass(str,strint) zip可选严格模式 zip新增可选参数strict,当该选项为True时,传入zip的两个可迭代项长度必须相等,否则将抛出ValueError 旧版(及不加此参数),当二者长度不等时,以长度较小的为准names〔a,b,c,d〕 numbers〔1,2,3〕 zzip(names,numbers) foreachinz: print(each) (a,1) (b,2) (c,3) 设置strict为True。。。 zzip(names,numbers,strictTrue) 。。。 d:projectspythonlearnPy310ahrefhttps:www。q578。coml110targetblankclassinfotextkey探索a。pyinmodule 3numbers〔1,2,3〕 4zzip(names,numbers,strictTrue) 5foreachinz: 6print(each) ValueError:zipargument2isshorterthanargument1 带括号的上下文管理器 with可以加括号了with(CtxManagerasexample): 。。。 with( CtxManager1, CtxManager2 ): 。。。 with(CtxManager1asexample, CtxManager2): 。。。 with(CtxManager1, CtxManager2asexample): 。。。 with( CtxManager1asexample1, CtxManager2asexample2 ): 。。。 如importpathlib ppathlib。Path p1ptext1。txt内容:文本1的内容 p2ptext2。txt内容:文本2的内容 with( p1。open(encodingutf8)asf1, p2。open(encodingutf8)asf2 ): print(f1。read,f2。read,sep) 文本1的内容 文本2的内容 显式类型别名 使用TypeAlias显式标注类型别名,提高可读性 旧的方式:xint defplusint(a:x,b:x)x: returnab 可以看到,x很容易被搞混 新的方式:使用TypeAlias表明这是个别名fromtypingimportTypeAlias x:TypeAliasint defplusint(a:x,b:x)x: returnab match。。。case语句 对,就是其他语言的switchcase,python终于提供了支持,还是加强版的 完整语法参见:PEP634StructuralPatternMatching:SpecificationPython。org〔3〕 举几个例子: 基本的类型匹配:day6 matchday: case1: print(星期一) case67: print(周末) case: print(其他情况) subject:这在处理命令行参数的时候特别有用 copyright:〔FunnySaltyFish〕(https:funnysaltyfish。github。io) date:2021100521:08:42 commandsave1。txt 试着把command改成listcopy1。txt2。txt看看效果 matchcommand。split(): case〔list〕: print(列出文件) case〔save,filename〕: print(f保存文件到{filename}) case〔copy,source,target〕: print(f拷贝{source}{target}) 也可以匹配对象:classPerson: pass classStudent(Person): definit(self,id:int)None: self。idid classTeacher(Person): definit(self,name:str)None: self。namename aStudent(1) aStudent(2) aTeacher(FunnySaltyFish) matcha: caseStudent(id2): print(f这是位学生,且id正好是2) caseStudent: print(f这是学生,id为{a。id}) caseTeacher: print(f这是老师,姓名为{a。name}) 当然也可以匹配字典:d{ name:李四,张三 age:18, hobby:阅读 } matchd: case{name:张三,args}: 收集其他参数 print(这是张三,args)这是张三{age:18,hobby:阅读} case{name:name,age:age,hobby:hobby}: print(f我叫{name},今年{age}岁,喜欢{hobby})我叫李四,今年18岁,喜欢阅读 更复杂的还有结合Guard、匹配捕获等使用,具体可以参见:PEP635StructuralPatternMatching:MotivationandRationalePython。org〔4〕和PEP636StructuralPatternMatching:TutorialPython。org〔5〕 更友好的报错提示 现在,当你的括号、引号未闭合时,python会抛出更加清晰明了的错误str未闭合的str Filed:projectspythonlearnPy310ahrefhttps:www。q578。coml110targetblankclassinfotextkey探索a。py,line90 str未闭合的str SyntaxError:unterminatedstringliteral(detectedatline90) arr〔1,2,2,3 Filed:projectspythonlearnPy310ahrefhttps:www。q578。coml110targetblankclassinfotextkey探索a。py,line91 arr〔1,2,2,3 SyntaxError:〔wasneverclosed 其他一些更新:distutils被弃用 推荐使用setuptools 需要OpenSSL1。1。1及以上版本移除PyUNICODE编码APIPyUnicodeObject的wstr被弃用,并将在之后移除 完。摸鱼去了。 参考资料 〔1〕 https:www。python。orgdownloadsreleasepython3100:https:link。juejin。cn?targethttps3A2F2Fwww。python。org2Fdownloads2Frelease2Fpython31002F 〔2〕 https:www。python。orgdownloadsreleasepython3100:https:link。juejin。cn?targethttps3A2F2Fwww。python。org2Fdownloads2Frelease2Fpython31002F 〔3〕 https:www。python。orgdevpepspep0634id25:https:link。juejin。cn?targethttps3A2F2Fwww。python。org2Fdev2Fpeps2Fpep06342F23id25 〔4〕 https:www。python。orgdevpepspep0635id15:https:link。juejin。cn?targethttps3A2F2Fwww。python。org2Fdev2Fpeps2Fpep06352F23id15 〔5〕 https:www。python。orgdevpepspep0636:https:link。juejin。cn?targethttps3A2F2Fwww。python。org2Fdev2Fpeps2Fpep06362F