3 個值得使用的首次亮相在 Python 3.0 中的特性
探索一些未被充分利用但仍然有用的 Python 特性。
這是 Python 3.x 首發(fā)特性系列文章的第一篇。Python 3.0 于 2008 年首次發(fā)布,盡管它已經(jīng)發(fā)布了一段時間,但它引入的許多特性都沒有被充分利用,而且相當酷。這里有三個你應(yīng)該知道的。
僅限關(guān)鍵字參數(shù)
Python 3.0 首次引入了僅限關(guān)鍵字參數(shù)參數(shù)的概念。在這之前,不可能指定一個只通過關(guān)鍵字傳遞某些參數(shù)的 API。這在有許多參數(shù),其中一些參數(shù)可能是可選的函數(shù)中很有用。
請看一個特意設(shè)計的例子:
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)
當閱讀調(diào)用該函數(shù)的代碼時,有時很難理解發(fā)生了什么:
show_arguments("hello", "extra")
base is hello
extended is extra
show_arguments("hello", None, "extra")
base is hello
improved is extra
雖然可以用關(guān)鍵字參數(shù)來調(diào)用這個函數(shù),但這明顯不是最好的方法。相反,你可以將這些參數(shù)標記為僅限關(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)
<ipython-input-7-6000400c4441> in <module>
----> 1 show_arguments("hello", "extra")
TypeError: show_arguments() takes 1 positional argument but 2 were given
對該函數(shù)的有效調(diào)用更容易預(yù)測:
show_arguments("hello", improved="extra")
base is hello
improved is extra
nonlocal
有時,函數(shù)式編程的人根據(jù)編寫累加器的難易程度來判斷一種語言。累加器是一個函數(shù),當它被調(diào)用時,返回目前為止發(fā)給它的所有參數(shù)的總和。
在 3.0 之前,Python 的標準答案是:
class _Accumulator:
def __init__(self):
self._so_far = 0
def __call__(self, arg):
self._so_far += arg
return self._so_far
def make_accumulator():
return _Accumulator()
雖然我承認有些啰嗦,但這確實有效:
acc = make_accumulator()
print("1", acc(1))
print("5", acc(5))
print("3", acc(3))
這樣做的輸出結(jié)果將是:
1 1
5 6
3 9
在 Python 3.x 中,nonlocal
關(guān)鍵字可以用少得多的代碼實現(xiàn)同樣的行為。
def make_accumulator():
so_far = 0
def accumulate(arg):
nonlocal so_far
so_far += arg
return so_far
return accumulate
雖然累加器是人為的例子,但使用 nonlocal
關(guān)鍵字使內(nèi)部函數(shù)擁有具有狀態(tài)的的能力是一個強大的工具。
擴展析構(gòu)
想象一下,你有一個 CSV 文件,每一行由幾個元素組成:
- 第一個元素是年份
- 第二個元素是月
- 其他元素是該月發(fā)表的全部文章數(shù),每天一個條目
請注意,最后一個元素是 文章總數(shù),而不是 每天發(fā)表的文章。例如,一行的開頭可以是:
2021,1,5,8,10
這意味著在 2021 年 1 月,第一天發(fā)表了 5 篇文章。第二天,又發(fā)表了三篇文章,使總數(shù)達到 8 篇。第三天,又發(fā)表了兩篇文章。
一個月可以有 28 天、30 天或 31 天。提取月份、日期和文章總數(shù)有多難?
在 3.0 之前的 Python 版本中,你可能會這樣寫:
year, month, total = row[0], row[1], row[-1]
這是正確的,但它掩蓋了格式。使用擴展析構(gòu),同樣可以這樣表達:
year, month, *rest, total = row
這意味著如果該格式改為前綴了一個描述,你可以把代碼改成:
_, year, month, *rest, total = row
而不需要在每個索引中添加 1
。
接下來是什么?
Python 3.0 和它的后期版本已經(jīng)推出了 12 年多,但是它的一些功能還沒有被充分利用。在本系列的下一篇文章中,我將會寫另外三個。