简体   繁体   English

Google Python风格指南

[英]Google Python style guide

Why does the Google Python Style Guide prefer list comprehensions and for loops instead of filter, map, and reduce? 为什么Google Python样式指南更喜欢列表推导和for循环而不是filter,map和reduce?

Deprecated Language Features: ... "Use list comprehensions and for loops instead of filter, map, and reduce. " 不推荐使用的语言功能:...“使用列表推导和循环而不是过滤,映射和减少。”

The explanation given : "We do not use any Python version which does not support these features, so there is no reason not to use the new styles." 给出的解释是:“我们不使用任何不支持这些功能的Python版本,因此没有理由不使用新的样式。”

map and filter are way less powerful than their list comprehension equivalent. mapfilter的功能不如它们的list comprehension等效。 LCs can do both filtering and mapping in one step, they don't require explicit function and can be compiled more efficiently because of their special syntax LC可以一步完成过滤和映射,它们不需要显式功能,并且由于其特殊的语法可以更有效地编译

# map and filter
map(lambda x:x+1, filter(lambda x:x%3, range(10)))
# same as LC
[x+1 for x in range(10) if x%3]

There is simply no reason to prefer map or filter over LCs. 没有理由更喜欢地图或过滤LC。

reduce is slightly different, because there is no equivalent LC, but it has no big advantage over a ordinary for-loop either. reduce略有不同,因为没有等效的LC,但它也没有比普通的for-loop更大的优势。

The Google Python Style guide does not say 谷歌Python风格指南没有说

prefer list comprehensions and for loops instead of filter, map, and reduce 更喜欢列表推导和循环而不是过滤,映射和减少

Rather, the full sentence reads, 相反,完整的句子读,

Use list comprehensions and for loops instead of filter and map when the function argument would have been an inlined lambda anyway . 当函数参数无论如何都是内联的lambda时,使用列表推导和for循环而不是filter和map。 (my emphasis) (我的重点)

So it is not recommending that you completely avoid using map , for instance -- only that 因此,不建议您完全避免使用map ,例如 - 仅限于此

[expression(item) for item in iterable] 

is preferable to 比较好

map(lambda item: expression(item), iterable)

In this case it is clear that the list comprehension is more direct and readable. 在这种情况下,清楚的是列表理解更直接和可读。

On the other hand, there is nothing wrong with using map like this: 另一方面,使用这样的map没有错:

map(str, range(100))

instead of the longer-winded 而不是啰嗦

[str(item) for item in range(100)]

It performs well to boot: 它很好地启动:

In [211]: %timeit list(map(str,range(100)))
7.81 µs ± 151 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [215]: %timeit [str(item) for item in range(100)]
10.3 µs ± 3.06 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

List comprehensions are generally considered more "pythonic" than filter , map and reduce . 列表推导通常被认为比filtermapreduce更“pythonic”。

See also this article by Python creator Guido van Rossum. 另请参阅Python创建者Guido van Rossum 撰写的这篇文章

As far as filing this under "Deprecated Language Features" in the style guide, there were apparently plans to deprecate filter , map and reduce in Python 3 (see the article referenced above). 至于在样式指南中的“弃用语言功能”下提交此内容,显然有计划在Python 3中弃用filtermapreduce (参见上面引用的文章 )。

Some of these plans changed eventually. 其中一些计划最终改变了。 reduce was dropped from being a built-in function (and moved to the functools module), but filter and map are still available as built-ins. reduce了从一个内置功能下降(并移动到functools模块),但filtermap仍然可用的内置插件。

I would think that it is because not everybody knows how to use those functions well; 我认为这是因为不是每个人都知道如何很好地使用这些功能; readability may be impaired for people who are not as familiar. 对于不那么熟悉的人,可读性可能会受损。 Also, the for loop and list comprehension are widely used and easy to understand; 此外, for循环和列表理解被广泛使用并且易于理解; even though the latter is from functional programming, just like map , filter , and reduce , it mirrors lists and for loops quite well. 即使后者来自函数式编程,就像mapfilterreduce ,它可以很好地镜像列表和for循环。 In any case, cramming a lambda or defining a function just to use with map, filter, or reduce can get annoying, especially since a lambda can only be a single expression and a function could clutter your code. 在任何情况下,填充lambda或定义一个仅用于map,filter或reduce的函数都会变得烦人,特别是因为lambda只能是单个表达式而函数可能会混乱你的代码。 You don't need them anyways; 反正你不需要它们; map(func, seq) is just [func(x) for x in seq] and filter is just a list comprehension with an if component. map(func, seq)只是[func(x) for x in seq] map(func, seq) [func(x) for x in seq]filter只是一个if组件的列表理解。 reduce can be done with a for loop. 可以使用for循环完成reduce

In short, for and list comprehensions are clearer, and they provide basically equivalent functionality in most cases. 简而言之, for和list comprehensions更清晰,并且在大多数情况下它们提供基本相同的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM