新聞中心
探索一些未被充分利用但仍然有用的 Python 特性。
創(chuàng)新互聯(lián)是一家專業(yè)提供臨城企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、H5頁(yè)面制作、小程序制作等業(yè)務(wù)。10年已為臨城眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
這是 Python 3.x 首發(fā)特性系列文章的第一篇。Python 3.0 于 2008 年首次發(fā)布,盡管它已經(jīng)發(fā)布了一段時(shí)間,但它引入的許多特性都沒(méi)有被充分利用,而且相當(dāng)酷。這里有三個(gè)你應(yīng)該知道的。
僅限關(guān)鍵字參數(shù)
Python 3.0 首次引入了僅限關(guān)鍵字參數(shù)參數(shù)的概念。在這之前,不可能指定一個(gè)只通過(guò)關(guān)鍵字傳遞某些參數(shù)的 API。這在有許多參數(shù),其中一些參數(shù)可能是可選的函數(shù)中很有用。
請(qǐng)看一個(gè)特意設(shè)計(jì)的例子:
def show_arguments(base, extended=None, improved=None, augmented=None):print("base is", base)if extended is not None:print("extended is", extended)if improved is not None:print("improved is", improved)if augmented is not None:print("augmented is", augmented)
當(dāng)閱讀調(diào)用該函數(shù)的代碼時(shí),有時(shí)很難理解發(fā)生了什么:
show_arguments("hello", "extra")base is helloextended is extrashow_arguments("hello", None, "extra")base is helloimproved is extra
雖然可以用關(guān)鍵字參數(shù)來(lái)調(diào)用這個(gè)函數(shù),但這明顯不是最好的方法。相反,你可以將這些參數(shù)標(biāo)記為僅限關(guān)鍵字:
def show_arguments(base, *, extended=None, improved=None, augmented=None):print("base is", base)if extended is not None:print("extended is", extended)if improved is not None:print("improved is", improved)if augmented is not None:print("augmented is", augmented)
現(xiàn)在,你不能用位置參數(shù)傳入額外的參數(shù):
show_arguments("hello", "extra")---------------------------------------------------------------------------TypeError Traceback (most recent call last)in ----> 1 show_arguments("hello", "extra")TypeError: show_arguments() takes 1 positional argument but 2 were given
對(duì)該函數(shù)的有效調(diào)用更容易預(yù)測(cè):
show_arguments("hello", improved="extra")base is helloimproved is extra
nonlocal
有時(shí),函數(shù)式編程的人根據(jù)編寫累加器的難易程度來(lái)判斷一種語(yǔ)言。累加器是一個(gè)函數(shù),當(dāng)它被調(diào)用時(shí),返回目前為止發(fā)給它的所有參數(shù)的總和。
在 3.0 之前,Python 的標(biāo)準(zhǔn)答案是:
class _Accumulator:def __init__(self):self._so_far = 0def __call__(self, arg):self._so_far += argreturn self._so_fardef make_accumulator():return _Accumulator()
雖然我承認(rèn)有些啰嗦,但這確實(shí)有效:
acc = make_accumulator()print("1", acc(1))print("5", acc(5))print("3", acc(3))
這樣做的輸出結(jié)果將是:
1 15 63 9
在 Python 3.x 中,nonlocal 關(guān)鍵字可以用少得多的代碼實(shí)現(xiàn)同樣的行為。
def make_accumulator():so_far = 0def accumulate(arg):nonlocal so_farso_far += argreturn so_farreturn accumulate
雖然累加器是人為的例子,但使用 nonlocal 關(guān)鍵字使內(nèi)部函數(shù)擁有具有狀態(tài)的的能力是一個(gè)強(qiáng)大的工具。
擴(kuò)展析構(gòu)
想象一下,你有一個(gè) CSV 文件,每一行由幾個(gè)元素組成:
- 第一個(gè)元素是年份
- 第二個(gè)元素是月
- 其他元素是該月發(fā)表的全部文章數(shù),每天一個(gè)條目
請(qǐng)注意,最后一個(gè)元素是 文章總數(shù),而不是 每天發(fā)表的文章。例如,一行的開(kāi)頭可以是:
2021,1,5,8,10
這意味著在 2021 年 1 月,第一天發(fā)表了 5 篇文章。第二天,又發(fā)表了三篇文章,使總數(shù)達(dá)到 8 篇。第三天,又發(fā)表了兩篇文章。
一個(gè)月可以有 28 天、30 天或 31 天。提取月份、日期和文章總數(shù)有多難?
在 3.0 之前的 Python 版本中,你可能會(huì)這樣寫:
year, month, total = row[0], row[1], row[-1]
這是正確的,但它掩蓋了格式。使用擴(kuò)展析構(gòu),同樣可以這樣表達(dá):
year, month, *rest, total = row
這意味著如果該格式改為前綴了一個(gè)描述,你可以把代碼改成:
_, year, month, *rest, total = row
而不需要在每個(gè)索引中添加 1。
接下來(lái)是什么?
Python 3.0 和它的后期版本已經(jīng)推出了 12 年多,但是它的一些功能還沒(méi)有被充分利用。在本系列的下一篇文章中,我將會(huì)寫另外三個(gè)。
分享名稱:3個(gè)值得使用的首次亮相在Python3.0中的特性
分享URL:http://www.fisionsoft.com.cn/article/copgdoo.html


咨詢
建站咨詢

