Python 3.10正式版釋出

語言: CN / TW / HK

Python在幾天前釋出了正式版3.10,雖然你不一定會馬上應用到生產環境,不過還是建議有條件的可以升級體驗以下,沒條件直接看我這篇文章就可以了,我列了幾個開發者可能比較感興趣的特性,看看哪個是你最期待的特性。

1、更友好的錯誤提示

expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4,             38: 4, 39: 4, 45: 5, 46: 5, 47: 5, 48: 5, 49: 5, 54: 6, some_other_code = foo()

比如這段程式碼如果你不小心漏掉一個右花括號,執行程式碼時,在之前的版本中直接提示語法錯誤:

File "example.py", line 3     some_other_code = foo()                     ^ SyntaxError: invalid syntax

不仔細檢查程式碼你還真的沒法一眼看出來到底哪裡語法錯誤。而在python3.10中,提示變得非常友好而且具體, 直接告訴你 "{"沒有關閉,這樣定位錯誤就很快了。

File "example.py", line 1     expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4,                ^ SyntaxError: '{' was never closed

類似地,還有推導式中如果忘記加圓括號時,之前一言不合直接提示語法錯誤

```

{x,y for x,y in zip('abcd', '1234')}   File "", line 1     {x,y for x,y in zip('abcd', '1234')}          ^ SyntaxError: invalid syntax ```

而現在會告訴你,是不是忘記加圓括號了。

```

{x,y for x,y in zip('abcd', '1234')}   File "", line 1     {x,y for x,y in zip('abcd', '1234')}      ^ SyntaxError: did you forget parentheses around the comprehension target? ```

嗯,這才人性化。

2、match ... case  終於來了

match ... case 語法是我比較期待的功能,它不是什麼多高階的功能,類似於其它語言中的 switch ... case 語法,在多條件判斷時比用 if ... elif 程式碼更簡潔。很難想象,這個語法現在才加進來,當然, 一開始Python之父是不願意加這個語法特性的,好在這個語法最終還是迴歸了,而且換了個名字。

我在想,幹嘛和自己過不去,統一都叫 switch ... case 不好嗎?也許這就是Python讓人著迷的地方吧。

來看個例子

這是用3.10的 match case 語法

def http_error(status):     match status:         case 400:             return "Bad request"         case 404:             return "Not found"         case 418:             return "I'm a teapot"         case _:             return "Something's wrong with the internet"

case _  類似於其它語言中的 default ,當其他條件都不符合就執行這行。

用普通的if ... else 語法來寫

def http_error(status):     if status == 400:         return "Bad request"     elif status == 404:         return "Not found"     elif status == 418:         return "I'm a teapot"     else:         return "Something's wrong with the internet"

3、支援括號的上下文管理器

在之前的老版本中,多個上下文管理器必須放在一行或者用轉義符“\”換行

``` with open("xxx.py", mode="w") as f1, open("yyy.py", mode="w") as f2:     pass

或者

with open("xxx.py", mode="w") as f1, \         open("yyy.py", mode="w") as f2:     pass ```

在3.10中,我們可以用括號將多個管理器放在多行,這樣程式碼看起來整潔一些。

with (     open("xxx.py", mode="w") as f1,     open("yyy.py", mode="w") as f2 ):     pass

4、新的型別聯合操作符

在之前版本中,對於函式引數如果希望型別支援多種,例如同時支援int和float,需要用Union:

``` from typing import Union

def foo(number: Union[ int, float]) -> Union[int, float]:     return number ** 2 ```

現在有個新的語法糖“|”,叫聯合操作符,可以讓程式碼更簡潔

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

該操作符在函式 isinstance()和 issubclass() 也可以支援

```

python3.10

isinstance(1, int | str) True

python3.7

isinstance(1, (int,float)) True ```

最後

當開發者問到Python是否還會有Python4.0的時候,Python之父直言不要對 Python 4.0 抱有希望。假如真的哪天釋出了Python4.0,也不會重走2.x過度到3.0的老路。同時,我們也別指望Python的GIL能夠去掉,不是沒嘗試過,而是去掉GIL之後更慢了。如果你的專案對效能非常敏感,不妨試試pypy,python的一個分支。

本文同步發表部落格:foofish.net

歡迎關注公眾號

http://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/216a486abacf4924b743a65f804af491~tplv-k3u1fbpfcp-zoom-1.image