Python 海象运算符 (:=) 的三种用法

  • 2019 年 11 月 28 日
  • 笔记

原标题 | Three Ways to Use the Walrus Operator in Python

作 者 | Jonathan Hsu

翻 译 | 人气呆毛选手

审 校 | 鸢尾、唐里

注:敬请点击文末【阅读原文】访问文中相关链接,PC查看体验更佳。

随着Python 3.8的发布,赋值表达式运算符(也称为海象运算符)也发布了。

运算符使值的赋值可以传递到表达式中。这通常会使语句数减少一个。例如:

  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()仅被调用一次,隐含的降低了调用次数。

处理返回的数据

接下来是我认为海象运算符的最有用的实现。我经常使用称为iFormBuilder的移动数据收集软件。具体来说,请求记录(数据行)后,查询返回数据时,需要对其进行处理。这是我每天编写的代码块:

  # 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)

就可读性而言,此重构的破坏性最小,实际上,我发现在这种情况下,海象运算符更易于阅读。与前两个示例形成鲜明对比的是,前两个示例对可读性的影响较小,有利于省心和提高性能。

总结

海象运算符很新颖并且有争议,但是只有时间会见证它逐渐被大家接收。你是否已开始在代码中使用它了呢?

via https://medium.com/better-programming/three-ways-to-use-the-walrus-operator-in-python-d5550f3a7dd