Space around operators and in list comprehensions in Python

2012-12-12

I disagree with PEP-8, that

 myarray[x/3 + 2:x/2 + 3:2]  # PEP-8 style

is better than

 myarray[x/3+2: x/2+3: 2]

or without whitespace in the slice expression at all.

I also think, that whitespace after opening and before closing brackets really make nested list comprehensions more readable (and better indented!). Compare

# PEP-8 style
foo_rows = [[foo_long_name(x, name, some, other, arguments)
             for x, name in zip(row, names)]
            for row in rows]

# the style I like more
foo_rows = [ [ foo_long_name(x, name, some, other, arguments)
               for x, name in zip(row, names) ]
             for row in rows]

In the second case I can clearly see the inner comprehension within the outer.