Google Python style guide

前端 未结 4 937
南旧
南旧 2020-11-30 01:44

Why does the Google Python Style Guide prefer list comprehensions and for loops instead of filter, map, and reduce?

Deprecated Language Features: ... \"Use list com

4条回答
  •  星月不相逢
    2020-11-30 02:07

    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; even though the latter is from functional programming, just like map, filter, and reduce, it mirrors lists and for loops quite well. 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. 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. reduce can be done with a for loop.

    In short, for and list comprehensions are clearer, and they provide basically equivalent functionality in most cases.

提交回复
热议问题