Python 3.8 有什麼新功能¶
- 編輯者:
Raymond Hettinger
本文介紹了 Python 3.8 與 3.7 相比多了哪些新功能。Python 3.8 已於 2019 年 10 月 14 日發布。有關完整詳細資訊,請參閱 changelog。
發布重點摘要¶
新增功能¶
Assignment expressions¶
There is new syntax := that assigns values to variables as part of a larger
expression. It is affectionately known as "the walrus operator" due to
its resemblance to the eyes and tusks of a walrus.
In this example, the assignment expression helps avoid calling
len() twice:
if (n := len(a)) > 10:
print(f"List is too long ({n} elements, expected <= 10)")
A similar benefit arises during regular expression matching where match objects are needed twice, once to test whether a match occurred and another to extract a subgroup:
discount = 0.0
if (mo := re.search(r'(\d+)% discount', advertisement)):
discount = float(mo.group(1)) / 100.0
The operator is also useful with while-loops that compute a value to test loop termination and then need that same value again in the body of the loop:
# Loop over fixed length blocks
while (block := f.read(256)) != '':
process(block)
Another motivating use case arises in list comprehensions where a value computed in a filtering condition is also needed in the expression body:
[clean_name.title() for name in names
if (clean_name := normalize('NFC', name)) in allowed_names]
Try to limit use of the walrus operator to clean cases that reduce complexity and improve readability.
完整敘述請見 PEP 572。
(由 Emily Morehouse 在 bpo-35224 中貢獻。)
Positional-only parameters¶
There is a new function parameter syntax / to indicate that some
function parameters must be specified positionally and cannot be used as
keyword arguments. This is the same notation shown by help() for C
functions annotated with Larry Hastings'
Argument Clinic tool.
In the following example, parameters a and b are positional-only, while c or d can be positional or keyword, and e or f are required to be keywords:
def f(a, b, /, c, d, *, e, f):
print(a, b, c, d, e, f)
以下是有效的呼叫:
f(10, 20, 30, d=40, e=50, f=60)
然而以下這些呼叫是無效的:
f(10, b=20, c=30, d=40, e=50, f=60) # b 不得為關鍵字引數
f(10, 20, 30, 40, 50, f=60) # e 必須為關鍵字引數
One use case for this notation is that it allows pure Python functions
to fully emulate behaviors of existing C coded functions. For example,
the built-in divmod() function does not accept keyword arguments:
def divmod(a, b, /):
"Emulate the built in divmod() function"
return (a // b, a % b)
Another use case is to preclude keyword arguments when the parameter
name is not helpful. For example, the builtin len() function has
the signature len(obj, /). This precludes awkward calls such as:
len(obj='hello') # The "obj" keyword argument impairs readability
A further benefit of marking a parameter as positional-only is that it
allows the parameter name to be changed in the future without risk of
breaking client code. For example, in the statistics module, the
parameter name dist may be changed in the future. This was made
possible with the following function specification:
def quantiles(dist, /, *, n=4, method='exclusive')
...
Since the parameters to the left of / are not exposed as possible
keywords, the parameters names remain available for use in **kwargs:
>>> def f(a, b, /, **kwargs):
... print