Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Python style guide

Tags:

Why does the Google Python Style Guide prefer list comprehensions and for loops instead of filter, map, and 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."

like image 684
Schitti Avatar asked Mar 24 '11 23:03

Schitti


People also ask

What is Python style guide?

Python is the main dynamic language used at Google. This style guide is a list of dos and don'ts for Python programs. To help you format code correctly, we've created a settings file for Vim. For Emacs, the default settings should be fine. Many teams use the yapf auto-formatter to avoid arguing over formatting.

What is PEP 8 -- style guide for Python code?

PEP 8, sometimes spelled PEP8 or PEP-8, is a document that provides guidelines and best practices on how to write Python code. It was written in 2001 by Guido van Rossum, Barry Warsaw, and Nick Coghlan. The primary focus of PEP 8 is to improve the readability and consistency of Python code.

What is the purpose of the Google style guide?

This style guide provides editorial guidelines for writing clear and consistent Google-related developer documentation. If you're new to the guide and looking for introductory topics about our style, then start with Highlights, Voice and tone, and Text-formatting summary.

What is code style guide?

Programming style, also known as code style, is a set of rules or guidelines used when writing the source code for a computer program. It is often claimed that following a particular programming style will help programmers read and understand source code conforming to the style, and help to avoid introducing errors.


2 Answers

map and filter are way less powerful than their list comprehension equivalent. 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

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

reduce is slightly different, because there is no equivalent LC, but it has no big advantage over a ordinary for-loop either.

like image 33
Jochen Ritzel Avatar answered Sep 21 '22 21:09

Jochen Ritzel


The Google Python Style guide does not say

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. (my emphasis)

So it is not recommending that you completely avoid using map, for instance -- only that

[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(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)
like image 115
unutbu Avatar answered Sep 22 '22 21:09

unutbu