<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Python 3.10剛發(fā)布,這5點非常值得學習!

          共 1853字,需瀏覽 4分鐘

           ·

          2021-10-17 07:25


          正值國慶節(jié)期間,Python官網(wǎng)發(fā)布了Python3.10.0。

          說實話,對于這次的升級,有幾個特性,還真是值得和大家講講。

          1. 更友好的錯誤提示

          Python 3.10以前,它是這樣提示的,你可能完全不知道哪里有問題,當代碼過多。

          print?("Hello"
          print?("word")

          ??File?".\test.py",?line?2
          ????print?("word")
          ????^
          SyntaxError:?invalid?syntax

          對于Python 3.10,它是這樣提示:

          File?".\test.py",?line?1
          ????print?("Hello"
          ??????????^
          SyntaxError:?'('?was?never?closed

          給你明確指示錯誤,太香了!

          2. zip新增可選參數(shù):嚴格模式

          zip新增可選參數(shù)strict, 當該選項為True時,傳入zip的兩個可迭代項長度必須相等,否則將拋出 ValueError。

          對于Python 3.10以前,沒有該參數(shù),當二者長度不等時,以長度較小的為準。

          names?=?["a","b","c","d"]
          numbers?=?[1,2,3]
          z?=?zip(names,numbers)
          for?each?in?z:
          ????print(each)

          結(jié)果如下:對于Python 3.10,設(shè)置strict為True。

          d:測試.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

          3. with可以加括號

          官方文檔中是這樣寫的:

          with?(CtxManager()?as?example):
          ????...

          with?(
          ????CtxManager1(),
          ????CtxManager2()
          ):
          ????...

          with?(CtxManager1()?as?example,
          ??????CtxManager2()):
          ????...

          with?(CtxManager1(),
          ??????CtxManager2()?as?example):
          ????...

          with?(
          ????CtxManager1()?as?example1,
          ????CtxManager2()?as?example2
          ):
          ????...

          這樣你一定看不懂,如果換成下面這種寫法呢?

          with(
          ????p1.open(encoding="utf-8")?as?f1,
          ????p2.open(encoding="utf-8")?as?f2
          ):
          ????print(f1.read(),?f2.read(),?sep="\n")?

          就是你現(xiàn)在可以一次性在with中,操作多個文檔了。

          4. 結(jié)構(gòu)化模式匹配:match...case...

          對,就是其他語言早就支持的的switch-case,Python今天終于提供了支持。

          day?=?7
          match?day:
          ????case?3:
          ????????print("周三")
          ????case?6?|?7:
          ????????print("周末")
          ????case?_?:?
          ????????print("其它")

          5. 新型聯(lián)合運算符

          以 X|Y 的形式引入了新的類型聯(lián)合運算符。

          def?square(x:?int|float):?
          ????return?x?**?2

          square(2.5)?
          #?結(jié)果:6.25

          新的運算符,也可用作 isinstance() 和 issubclass() 的第二個參數(shù)。

          #?True
          isinstance("a",?int|str)

          #?True?
          issubclass(str,?str|int)

          推薦閱讀


          牛逼!Python常用數(shù)據(jù)類型的基本操作(長文系列第①篇)

          牛逼!Python的判斷、循環(huán)和各種表達式(長文系列第②篇)

          牛逼!Python函數(shù)和文件操作(長文系列第③篇)

          牛逼!Python錯誤、異常和模塊(長文系列第④篇)


          瀏覽 33
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  极品人妻疯狂3p超刺激 | 韩国一级视频 | 99re成人精品视频免费看 | 国产婷婷欧美 | 国产操视频在线 |