器之心整理
參與:一鳴、路
Python 是一門廣受好評的編程語言,每個版本的更新都會對開發(fā)社區(qū)帶來一定影響。近日,Python 3.8 已進入 beta 2 版本的測試中,各項新特性已經(jīng)添加完畢,最終版本預計于今年 10 月發(fā)布。在發(fā)布即將到來前,機器之心總結(jié)了 Python 3.8 中幾大值得關(guān)注的新功能和改進。
從事計算機領(lǐng)域工作的讀者朋友對 Python 編程語言應該非常熟悉了。這是一門廣受好評的動態(tài)編程語言,其靈活和語法簡易的特點使得這門語言在腳本工具、數(shù)據(jù)分析、Web 后端都有廣泛的應用。Python 開發(fā)社區(qū)也非常活躍,3.x 的版本迭代速度非常快。2018 年 6 月底,Python 3.7 問世,之后 Python 3.8 的開發(fā)和測試工作也已經(jīng)展開。近日,Python 軟件基金會公開了 3.80b2 的說明文檔,向公眾展示了 beta 版本的測試進展,以及 Python 3.8 版本的新特性和功能改進。
目前,Python 3.8 的 beta 測試流程正在進行中。今年 6 月初,官方發(fā)布了第一個 beta 版本——Python 3.80b1,緊接著一個月后發(fā)布了 Python 3.80b2。第二個 beta 版本發(fā)布后,Python 3.8 新特性已經(jīng)添加完畢。官方目前已公布最終版本的發(fā)布時間,預計在今年的 10 月份。
那么,新的 Python 3.8 版本有哪些新特性和功能呢?機器之心根據(jù) Python 基金會公開的文檔,整理出了以下值得期待的新特性和功能改進。
文檔地址:https://docs.python.org/zh-cn/3.8/whatsnew/3.8.html#summary-release-highlights
新的語法
Python 3.8 中最值得關(guān)注的部分在于其語法的變化,這些新語法有助于提升效率,減少代碼工作量。
海象運算符( :=)
這個「:=」橫過來看是不是有點像海象的臉?這是一個新的 Python 語法,可以在進行條件判斷時直接為變量賦值。
過去我們需要首先對某個變量進行賦值,然后進行條件判斷。
m=re.match(p1, line)if m: return m.group(1)else: m=re.match(p2, line)if m: return m.group(2)else: m=re.match(p3, line) ...
而使用海象運算符后,我們可以直接為變量賦值:
if m :=re.match(p1, line): return m.group(1)elif m :=re.match(p2, line): return m.group(2)elif m :=re.match(p3, line):
還有一個在循環(huán)中使用的例子,過去在對某個變量進行循環(huán)前必須首先賦值:
ent=obj.next_entry()while ent: ... # process ent ent=obj.next_entry()
現(xiàn)在可以一邊循環(huán)一邊賦值:
while ent :=obj.next_entry(): ... # process ent
代碼調(diào)試中支持 f-string
f-string(或者稱為「格式化字符串」)在 Python 3.6 版本中加入的,雖然這一特性非常方便,但是開發(fā)者發(fā)現(xiàn) f-string 對調(diào)試沒有幫助。因此,Eric V. Smith 為 f-string 添加了一些語法結(jié)構(gòu),使其能夠用于調(diào)試。
在過去,f-string 可以這樣使用:
print(f'foo={foo} bar={bar}')
在 Python 3.8 中,只需使用如下代碼(更加簡潔):
print(f'{foo=} {bar=}')
兩種情況下,輸出都是:
>>> foo=42>>> bar='answer ...'>>> print(f'{foo=} {bar=}') foo=42 bar=answer ...
此外,可以通過在賦值符號后增加「!s」和「!f」命令,規(guī)定輸出結(jié)果的格式,例如:
>>> import datetime>>> now=datetime.datetime.now()>>> print(f'{now=} {now=!s}') now=datetime.datetime(2019, 7, 16, 16, 58, 0, 680222) now=2019-07-16 16:58:00.680222
>>> import math>>> print(f'{math.pi=!f:.2f}') math.pi=3.14
如代碼所示,第二個「now」變量的等號后增加了「!s」字符,使得 now 從「datetime」類型的輸出變成了字符串類型的輸出。在「pi」的輸出中,由于加入了「!f:.2f」,使得 pi 的輸出變?yōu)榱吮A粜?shù)點后兩位。
而花括號中的格式也會影響打印結(jié)果的格式,例如:
>>> a=37>>> print(f'{a=}, {a=}') a=37, a=37
花括號中的等號前后間距不同,打印結(jié)果中的間距也不一樣。
不需要 Keyword 的變量輸入
這一語法使得函數(shù)在定義輸入變量時可以規(guī)定只能輸入 Value,而不在 Value 前加上 Keyword。
例如,在使用 pow() 函數(shù)時:
>>> pow(2, 3) 8>>> pow(x=2, y=3) ... TypeError: pow() takes no keyword arguments
第一種方法是合法的,但是第二種則非法。
為了保證第一種純粹的 Python 函數(shù)方法,開發(fā)者可以在定義函數(shù)時使用「/」規(guī)定哪些變量的輸入必須按照第一種格式。例如:
def pow(x, y, z=None, /): r=x**y if z is not None: r %=z return r
在定義了所有變量之后額外增加一個「/」字符,表示所有的 Python 變量輸入必須按照 pow(x, y, z) 的方式進行。
當然,也可以在變量之間插入「/」,正斜杠之前的變量按照純粹的 Python 輸入方法,而正斜杠之后的按照定義好的方法執(zhí)行。例如,定義如下函數(shù):
def fun(a, b, /, c, d, *, e, f): ...
則以下一些表達式合法的,但另一些非法:
fun(1, 2, 3, 4, e=5, f=6) # legalfun(1, 2, 3, d=4, e=5, f=6) # legalfun(a=1, b=2, c=3, d=4, e=5, f=6) # illegal
其他特性
除了一些語法方面的改進,Python 3.8 版本還有一些其他的變化。
可移動的「__pycache__」
__pycache__目錄是由 Python3 解釋器創(chuàng)建的,用于保存.pyc 文件。這些文件保存著解釋器編譯.py 文件之后的字節(jié)碼(byte code)。之前的 Python 版本僅僅只是為每個.py 文件創(chuàng)建一個.pyc 文件,但是新版本會有所變化。
為了支持多版本的 Python,包括一些不是 CPython 的版本(如 PyPy),現(xiàn)在庫文件會為每個 Python 版本創(chuàng)建對應的.pyc 文件,格式形如「name.interp-version.pyc」。例如,某個 foo.py 文件在第一次使用的時候會創(chuàng)建一個對應的 pyc 文件,其路徑為「__pycache__/foo.cpython-37.pyc」,這個 pyc 文件會定義使用的 Python 版本。
其他改進
Python 3.8 會為 C 語言擴展加入更快的調(diào)用方式,這種方式原本屬于 CPython。在 Python3.8 中,這一功能是實驗性的,最終的完成版本會出現(xiàn)在 Python3.9 中。
同時,編譯器中的初始化配置處理也得到了清理,使得 Python 可以更好地嵌入其他程序,不需要依賴環(huán)境變量或在已有的 Python 系統(tǒng)中增加導致沖突的其他組件。
此外,大量的 Python 自帶模塊得到了改進和調(diào)整,如「os.path」、「shutil」、「math」、「ssl」等。
Python 3.8 什么時候與大家見面?
目前發(fā)布時間仍在探討,但考慮到 Python 3.9 的發(fā)布計劃(大約在 2020 年 6 月),官方認為 Python3.8 的發(fā)布時間不應當晚于今年 10 月份。
0月14日,Python 3.8 正式版發(fā)布。這也意味著一個Python開發(fā)周期的結(jié)束,和另一個開發(fā)周期的開始。Python 3.9 預計在2020年10月份發(fā)布。
那么Python 3.8都帶來了什么新特性和什么新改動呢?讓我們來盤點一下。
1、賦值表達式
顧名思義,就是在表達式中進行賦值。這個特性總體來說是一個語法糖,讓代碼看起來更簡潔了一點點。比如,下面這段代碼:
在Python 3.8中可以寫為:
如果n這個變量只有在if語句及子句中使用,那么明顯Python 3.8的寫法更為簡潔。其中Python 3.8中用到的操作符:=被稱為海象操作符(因為這個操作符看起來像海象的眼睛和一對兒長牙)。
嗯,你看,像不像?
這個特性的另外兩個典型例子是在while語句,以及列表推導中,像這樣:
2、限定位置參數(shù)
這個特性允許你通過在函數(shù)參數(shù)列表中加一個"/",來表示"/"之前的參數(shù)只能夠當作位置參數(shù)使用。下面我們看一個例子:
這個例子中,a、b只能當作位置參數(shù)使用,e、f只能當作關(guān)鍵字參數(shù)使用(如果你不知道為什么,請復習*和**的用法),而c、d即可以用作位置參數(shù),也可以用作關(guān)鍵字參數(shù)。
這個特性主要是庫或者基礎(chǔ)代碼的作者使用,它讓基礎(chǔ)代碼的作者不必為參數(shù)起一個固定的名字,參數(shù)名字調(diào)整也不會破壞調(diào)用者的代碼。沒有這個特性之前,有的調(diào)用者會對每個參數(shù)都以關(guān)鍵字參數(shù)的形式調(diào)用,這就導致了庫作者一改參數(shù)名,調(diào)用代碼就崩潰。
另外,還有的庫函數(shù)根本不適合用名字傳參,比如我們常見的add函數(shù),add(1, 2)明顯表意性要好于add(a=1, b=2),add的參數(shù)無論起什么名字,都感覺是多余的。
3、支持將編譯字節(jié)碼文件輸出到其他位置
Python項目中一個常見的麻煩就是清理git代碼庫中的__pycache__文件,這些文件往往是你一不小心就添加進去了,清理起來卻頗為麻煩。Python 3.8支持通過設(shè)置環(huán)境變量 PYTHONPYCACHEPREFIX ,讓__pycache__生成到其他位置,而不跟.py文件混在一起。你在代碼中可以通過 sys.pycache_prefix 查看生成的位置。
4、F字符串內(nèi)的'='表達式
在調(diào)試的時候,你是否經(jīng)常會寫這樣的代碼?
其中user={user},member_since={member_since}就是典型的樣板代碼。在Python 3.8中,你可以簡寫為:
一個等號,f字符串就知道你需要在等號后面繼續(xù)把等號前面變量的值顯示出來。對于調(diào)試來說,這確實更加簡潔了。
5、pickle協(xié)議5,獨立處理內(nèi)存中的數(shù)據(jù)緩存。
Python進程之間傳輸數(shù)據(jù)時,pickle是經(jīng)常用到的。對于數(shù)據(jù)量大的情況,妥善管理并優(yōu)化內(nèi)存使用是非常重要的。Python3.8 中對pickle的數(shù)據(jù)傳輸做了優(yōu)化,提高了性能和效率。
1、finally語句中可以有continue子句了,以前是不行的。
2、函數(shù)調(diào)用時,f((keyword)=arg) 這種寫法不再允許了。
3、新增模塊importlib.metadata,用以讀取第三方包的信息。
◆ ◆ ◆ ◆ ◆
以上就是小編總結(jié)的大家應該會比較感興趣的特性和改動了,如果您想查看完整的changelog,請訪問這個地址(點擊閱讀原文也可以):
https://docs.python.org/3.9/whatsnew/3.8.html
理了23個跟傳統(tǒng)歷史文化有關(guān)的網(wǎng)站,千萬不要錯過~
1.故宮博物院藏品總目:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=https%253A%252F%252Fzm-digicol.dpm.org.cn%252F&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:https%3A//zm-digicol.dpm.org.cn/}
2.漢典古籍:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FGt1aM&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/Gt1aM}
3.中國古籍網(wǎng):{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=https%253A%252F%252Fc.pc.qq.com%252Fmiddlem.html%253Fpfurl%253Dhttp%25253A%25252F%25252Ft.cn%25252FzOqoclW%2526gjsublevel%253D2804%2526pfuin%253D83781672%2526pfto%253Dmqq.qzone%2526type%253D0%2526gjlevel%253D15&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:https%3A//c.pc.qq.com/middlem.html?pfurl=http%253A%252F%252Ft.cn%252FzOqoclW&gjsublevel=2804&pfuin=83781672&pfto=mqq.qzone&type=0&gjlevel=15}
4.中國歷史地圖集:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FzOZ8DRT&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/zOZ8DRT}
5.中國傳統(tǒng)顏色網(wǎng)站:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Fzhongguose.com%252F&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//zhongguose.com/}
6.歷代詩人地域分布:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FESIy0mp&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/ESIy0mp}
7.中國歷代人物圖像數(shù)據(jù)庫:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FzOZns18&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/zOZns18}
8.中國地方志數(shù)據(jù)庫:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Flcd.ccnu.edu.cn%252F%2523%252Findex&src_uin=2941347079&src_scene=7035&cli_scene=getDetail#/index,text:http%3A//lcd.ccnu.edu.cn/#/index}
9.國學導航:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=https%253A%252F%252Fc.pc.qq.com%252Fmiddlem.html%253Fpfurl%253Dhttp%25253A%25252F%25252Ft.cn%25252FRyhWagD%2526gjsublevel%253D2804%2526pfuin%253D83781672%2526pfto%253Dmqq.qzone%2526type%253D0%2526gjlevel%253D15&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:https%3A//c.pc.qq.com/middlem.html?pfurl=http%253A%252F%252Ft.cn%252FRyhWagD&gjsublevel=2804&pfuin=83781672&pfto=mqq.qzone&type=0&gjlevel=15}
10.古籍館:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=https%253A%252F%252Fwww.gujiguan.com%252F&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:https%3A//www.gujiguan.com/}
11.中醫(yī)古籍全文數(shù)據(jù)庫:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FRLXw2ko&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/RLXw2ko}
12.全歷史:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=https%253A%252F%252Fwww.allhistory.com%252F&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:https%3A//www.allhistory.com/}
13.國學大師:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Fwww.guoxh&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//www.guoxh}ttp://t.cn/A6q84dGA
14.中國京劇戲考:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FA6q84dGA&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/A6q84dGA}
15.書法空間:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FRxQPAqc&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/RxQPAqc}
16.發(fā)現(xiàn)中國:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FRYI2Iaj&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/RYI2Iaj}
17.中國歷史學習網(wǎng):{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FA6ICUI2U&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/A6ICUI2U}
18.讀典籍:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FA655he6S&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/A655he6S}
19.古今文字集成:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=https%253A%252F%252Fc.pc.qq.com%252Fmiddlem.html%253Fpfurl%253Dhttp%25253A%25252F%25252Ft.cn%25252FRLdkvFV%2526gjsublevel%253D2804%2526pfuin%253D83781672%2526pfto%253Dmqq.qzone%2526type%253D0%2526gjlevel%253D15&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:https%3A//c.pc.qq.com/middlem.html?pfurl=http%253A%252F%252Ft.cn%252FRLdkvFV&gjsublevel=2804&pfuin=83781672&pfto=mqq.qzone&type=0&gjlevel=15}
20.歷史劇里看歷史:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252F8kIgi9a&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/8kIgi9a}
21.成語查詢:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FhRDRm&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/hRDRm}
22.搜韻:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=https%253A%252F%252Fwww.sou-yun.cn%252F&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:https%3A//www.sou-yun.cn/}
23.唐宋文學編年地圖 :{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=https%253A%252F%252Fsou-yun.cn%252FMPoetLifeMap.aspx&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:https%3A//sou-yun.cn/MPoetLifeMap.aspx}
「整理不易 路過的伙伴們點個贊 喜歡的關(guān)注收藏轉(zhuǎn)發(fā)」
*請認真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。