Python3.8 :=海象运算符简单介绍

海象运算符python 3.8支持

1.实现赋值

EXPERIMENTAL = 123

if c_o := EXPERIMENTAL:
    print(c_o)
    
# 结果:
# 123

2. 避免len()方法运行两次,提高了运行速度.

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

 

posted @ 2021-07-11 19:48  mik99  阅读(183)  评论(0编辑  收藏  举报