简体   繁体   中英

When is it ok to use whitespace in Python to improve readability?

I come from a background of other languages that use whitespace heavily to improve readability. I typically see lines of python code like the following:

my_data_1 = {'state': ['Alabama', 'Alaska'], 'abbv': ['AL', 'AK'], 'area': [52423, 656424], 'pop': [4040587, 550043]}

I find it difficult to scan quickly and recognize the data structure, especially when it gets more complicated than this. I know Python is touchy about whitespace indentation, so I tried the following two other options:

my_data_2 = {'state': ['Alabama', 'Alaska'],
             'abbv': ['AL', 'AK'],
             'area': [52423, 656424],
             'pop': [4040587, 550043]}

my_data_3 = {'state': ['Alabama', 'Alaska'],
             'abbv' : ['AL',      'AK'],
             'area' : [52423,     656424],
             'pop'  : [4040587,   550043]}

I was surprised that they worked just like the single-line version.

I understand how indentation replaces the C-like {...} for code blocks, but I can't find any documentation on when whitespace is ignored.

When is it ok to use whitespace for improving readability?

Python is not particularly touchy about whitespace; it's touchy about indentation when that indentation affects parsing, specifically, indentation that precedes the beginning of a statement . Other whitespace is generally ignored beyond its use to separate other tokens. This includes:

  • Whitespace inside the {...} of either dict or set literal.
  • Whitespace inside the [...] of a list literal.
  • Whitespace inside any parenthesized expression.
  • Whitespace at the beginning of the continuation of a line:

     this = whitespace \\ + does \\ + not \\ + count 

PEP-8 would suggest formatting like the following:

my_data_2 = {
    'state': ['Alabama', 'Alaska'],
    'abbv': ['AL', 'AK'],
    'area': [52423, 656424],
    'pop': [4040587, 550043]
}

It's:

my_data_2 = {
    "state": ["Alabama", "Alaska"],
    "abbv": ["AL", "AK"],
    "area": [52423, 656424],
    "pop": [4040587, 550043],
}

How do I know?

black told me so.

From the zen of python:

Readability counts.

In regards to when it is acceptable, I believe vertically aligning code helps with readability, and this is generally an opinionated topic. Some hate vertically aligned code. If it helps you read it, then it works (unless it has to get code reviewed).

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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