运算符使值的赋值可以传递到表达式中。这通常会使语句数减少一个。例如:
my_list=[1,2,3]
count=len(my_list)
if count>3:
print(f"Error,{count}is too many items")
#when converting to walrus operator...
if(count:=len(my_list))>3:
print(f"Error,{count}is too many items")
如果您快速浏览了一下,就会理解对这个新操作的担忧。似乎将两个简单的语句混为一谈,以使收益最小。
假设对于为什么引入此运算符有合理答案?那么让人很自然的想到,它在现实世界中如何有用?
下面我们介绍下采用海象运算符的方便之处。
While循环
虽然循环似乎是最常见的使用海象运算符的情况。在此处使用海象运算符的好处是可以在while语句中合并表达式和修饰符。
line=f.readLine()
while line:
print(line)
line=f.readLine()
#when converting to walrus operator...while line:=f.readLine():
print(line)
不过要注意,使用海象运算符更适合用于传统的do/while循环,因为赋值在循环表达式之前。请参见下面的差异:
n=0
while n<3:
print(n)#0,1,2
n+=1
#when converting to walrus operator...
w=0
while(w:=w+1)<3:
print(w)#1,2
会发现它在替换无限while循环中最有用:
while True:
p=input("Enter the password:")
if p=="the password":
break
#when converting to walrus operator...
while(p:=input("Enter the password:"))!="the password":
continue
能够将while循环转换为一个语句实在太棒啦。
列表理解
海象运算符对优化某些列表理解语句很有帮助。有两个标准1)需要过滤数据,2)存储taxing function的结果。让我们看一下传统的列表理解语句:
scores=[22,54,75,89]
valid_scores=[
longFunction(n)
for n in scores
if longFunction(n)
]
注意条件语句longFunction(n)了吗?注意longFunction()被调用了两次吗?这是海象运算符的候选。
scores=[22,54,75,89]
valid_scores=[
result for n in scores
result:=longFunction(n)
]
在优化了的代码中,longFunction()仅被调用一次,隐含的降低了调用次数。
处理返回的数据
接下来是海象运算符的最有用的实现。具体来说,请求记录(数据行)后,查询返回数据时,需要对其进行处理。
#look for failed inspections
#if there are failed inspections,assign to technicianrecords=api.readFailedRecords()
if len(records)>0:
for record in records:
api.assignToTechnician(record)
仔细阅读记录,认真谨慎的敲代码,以确保仅当返回列表有数据该代码块才执行:
if records:=api.readFailedRecords():
for record in records:
api.assignToTechnician(record)
就可读性而言,此重构的破坏性最小,实际上,在这种情况下,海象运算符更易于阅读。与前两个示例形成鲜明对比的是,前两个示例对可读性的影响较小,有利于省心和提高性能。
总结
海象运算符很新颖并且有争议,但是只有时间会见证它逐渐被大家接收。你是否已开始在代码中使用它了呢?