<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的幾個好用的新特性

          共 2673字,需瀏覽 6分鐘

           ·

          2021-10-20 12:32

          來源:Deephub Imba

          本文約1200字,建議閱讀5分鐘?

          本文為你介紹Python 3.10新的有用的特性。?


          3.10版沒有添加新的模塊,但是引入了很多新的并且有用的特性。讓我們來一起看看吧。

          更詳細(xì)語法錯誤提示信息


          在調(diào)試代碼時,能夠提供更多的錯誤新信息和提示了,報告可以指出錯誤的語法,而不僅僅是提供“Syntax Error”。

          這是個新的特性簡直是太好了,不僅對于剛開始學(xué)習(xí)python的人來說對于所有人都是一個好消息,再也不需要去一個字一個字的看了。python 3.10會提示我們錯誤的位置,再也不需要StackOverflow了,哈。


          結(jié)構(gòu)模式匹配


          結(jié)構(gòu)模式匹配以 match 語句和 case 語句的形式使用。模式可以是序列、映射、python原始數(shù)據(jù)類型和類實例。我們可以把它看作switch 語句的加強(qiáng)版, 一個簡單的例子:

          numbers = [1,2,3,4]for n in numbers:  match n:      case 1:          print("Number is 1")      case 2:          print("Number is 2")      case 3:          print("Number is 3")      case _:          print("Number is not 1,2 or 3")

          關(guān)鍵字match后跟變量名稱。如果匹配,則將執(zhí)行 case 塊內(nèi)的語句。如果沒有匹配,則執(zhí)行 case _ 塊內(nèi)的語句。

          上面的程序結(jié)果如下所示:

          Number is 1Number is 2Number is 3Number is not 1,2 or 3

          這種結(jié)構(gòu)模式匹配還適用于復(fù)雜模式。

          def human_age(person): # person = (name, age, gender)  match person:      case (name, _, "male"):          print(f"{name} is man.")      case (name, _, "female"):          print(f"{name} is woman.")      case (name, age, gender):          print(f"{name} is {age} old.")

          結(jié)果是這樣的:

          human_age(("Carol", 25, "female"))Carol is woman.

          很像Scala啊,使用3.10的Pyspark應(yīng)該會簡單很多,這里貼個Scala代碼,看看是不是很像

          import scala.util.Random
          val x: Int = Random.nextInt(10)
          x match {case 0 => "zero"case 1 => "one"case 2 => "two"case _ => "other"}

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


          以 X|Y 的形式引入了新的類型聯(lián)合運算符。這提供了表達(dá) X 型或 Y 型的清晰形式。

          def square(number: int|float):  return number ** 2

          結(jié)果:

          square(2.5)6.25

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

          isinstance("3",int|str)Trueisinstance("GoodBye",int|str)True

          現(xiàn)有模塊的一些改進(jìn)


          pprint() 添加了一個新的關(guān)鍵字參數(shù)-underscore_numbers。

          >>> pprint.pformat(int(1e9),underscore_numbers=True)'1_000_000_000'

          我們可以使用int.bit_count() 來計算整數(shù)的二進(jìn)制表示中的位數(shù)。

          value = 50print(bin(value))0b101010print(value.bit_count())3

          有點像numpy的bincount啊

          Statistics增加了協(xié)方差函數(shù)

          >>> import statistics>>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9]>>> y = [1, 2, 3, 1, 2, 3, 1, 2, 3]>>> statistics.covariance(x,y)0.75

          statistics.correlation()還能計算Pearson相關(guān)系數(shù)。

          這倆我沒想明白為什么要加,可能我作為AI開發(fā),np,scipy和pandas都是必裝,web開發(fā)也用不到這些吧,實在沒懂加這個是為什么。

          statistics.linear_regression()這個就好玩了,雖然也沒什么用但是statistics可以計算線性回歸了。

          根據(jù)此線性函數(shù)描述自變量 x 和因變量 y 之間的關(guān)系:-

          y = 斜率 * x + 截距 + 噪聲

          其中斜率和截距是估計的回歸參數(shù),噪聲表示數(shù)據(jù)的可變性。

          >>> years = [2001,2005,2010]>>> houses_built = [5,8,14]>>> slope, intercept = statistics.linear_regression(years, houses_built)
          >>> round(slope * 2017 + intercept)21

          下一步加個梯度的反向傳播,statistics模塊就可以訓(xùn)練MLP了,哈


          總結(jié)


          其他的像Cpython之類的我覺得關(guān)系不大的就不細(xì)說了,3.10的語法提示真是太香了,絕對可以節(jié)省不少的開發(fā)調(diào)試時間,模式匹配也是個好東西,用過Scala的都知道。

          最后就是我們到底要不要升級呢?對于我來說,我的python版本是根據(jù)Pytorch走的,Pytorch支持哪版我就用那版。雖然有Conda可以管理版本,但是還有很多其他的Python包還不知道支持不支持3.10,所以我的建議是再等等看吧,畢竟我們是拿來用的自己用的舒服才好。但是要是不考慮其他包兼容的問題3.10我肯定優(yōu)先升級。

          編輯:于騰凱
          瀏覽 88
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

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

          手機(jī)掃一掃分享

          分享
          舉報
          <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>
                  国产伦子伦一级A片视 | 青榴最新入口 | 美女裸体91 | 色中色在线视频 | 天堂俺去俺来也www色官网 |