python 3.8正式版更新之后,海象运算符获得正式python版本的支持。
官网介绍:
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.
翻译:
这是一种新的语法,它是一个强大的表达式,赋值语句会被当做表达式的一部分。由于这个操作符很像海象的眼睛和象牙,所以我们亲切的称之为“海象操作符”。
官网给了一些例子:
if (n := len(a)) > 10:
print(f<