Python 3.8 的新功能

编者 :

Raymond Hettinger

This article explains the new features in Python 3.8, compared to 3.7. Python 3.8 was released on October 14, 2019. For full details, see the changelog .

摘要 – 发行亮点

新特征

赋值表达式

有新句法 := 将值赋值给变量作为更大表达式的一部分。它被亲切称为 "海象运算符",由于形似 海象的眼睛和长牙 .

在此范例中,赋值表达式帮助避免调用 len() 两次:

if (n := len(a)) > 10:
    print(f"List is too long ({n} elements, expected <= 10)")