Python: using 4 spaces for indentation. Why? [clos

2019-01-16 10:51发布

While coding python I'm using only 2 spaces to indent, sure PEP-8 really recommend to have 4 spaces, but historically for me it's unusual.

So, can anyone convince me to use 4 spaces instead of 2? What pros and cons?

P.S. And finally, what's easy way to convert all existing codebase from 2 spaces to 4 spaces?


P.P.S. PEP-8 Also srictly recommend not using tabs for indention. read here


So, to summarize:

Pros:

  • Have more space to arrange when wraping string more than 80 lines long.
  • Can copy code from snippets and it just works.

Cons:

  • With deeper level of nested statements you have less space for actual code.

Thanks.

13条回答
贪生不怕死
2楼-- · 2019-01-16 11:14

One reason is that if you use less spaces for indentation, you will be able to nest more statements (since line length is normally restricted to 80).

Now I'm pretty sure that some people still disagree on how many nested constructs should be the maximum.

查看更多
等我变得足够好
3楼-- · 2019-01-16 11:15

If you want to write python code together with other programmers it becomes a problem if you use a different indention as them. Most Python programmers tend to use 4-space indention.

查看更多
迷人小祖宗
4楼-- · 2019-01-16 11:20

I like the fact that four space characters nicely indents the inner code of a function, because def + one space makes four characters.

def·foo():
····pass
查看更多
姐就是有狂的资本
5楼-- · 2019-01-16 11:20

I think the real question is why spaces and not tabs.

Tabs are clearly better:

  • It makes nearly impossible to have inconsistent indentation (I've seen code that normally has 4 spaces indents, but then some parts happen to be one space off, it's difficult to tell by simple inspection if there are 7 or 8 spaces... That wouldn't happen with tabs, unless you set your tabstop to 1 space).
  • Tab is a logical semantic representation for indentation, it allows you (and any other developer) to choose to display as many "spaces" (or rather columns) you want without messing with other people's preferences.
  • It is also less keystrokes if you happen to have only "notepad" (or other dummy editor) at hand.
  • Adding and removing tabs is a symmetric operation. Most IDE's may insert automatically 4 spaces when hitting the tab key, but usually they remove just 1 space when hitting backspace (un-indent operation is still accessible as shift-tab, but that's a two key combination) or you use the mouse to click in the middle of the indentation and delete one character.
  • They take only 1 byte rather than 4 (multiply by thousands of lines and you save a few KB! :p)
  • You have one less thing to settle an agreement, because if you decide to go for spaces, then the discussion starts again to choose how many (although the consensus seems to be around four).

Advantages of spaces:

  • Guido likes them.
  • You cannot easily type a tab here, it transfers the focus (although you can paste one).
查看更多
beautiful°
6楼-- · 2019-01-16 11:23

Any decent editor (emacs, vim) will abstract this whole nonsense out for you. It will work equally well with spaces or tabs, and it can be configured to use any number of spaces (or any number of space-widths for a tab character). It can also convert between the different formats without too much trouble (see the :retab command in vim).

If you're trying to convert source formatting in bulk, I recommend you take a look at the indent utility.

That said, I can't resist answering the other question... My preference has always been for tabs, since it bypasses the whole issue and everyone can view the source code with the widths set as they see fit. It's also a lot less typing when you're working in editors that aren't helpful with converting it. As far as 2 vs 4 spaces, that's purely cosmetic.

查看更多
乱世女痞
7楼-- · 2019-01-16 11:26

There's no "better" indentation. It's a religious holy-war topic. Four is nice because it's enough to make the indentation clear, but not so much that your whole screen is mostly whitespace and you have to scroll horizontally to read half the program.

It also has the upside of being a "half-tab" w/r to the historical definition of a "tab."

Other than that, use whatever your group likes. It's like chocolate vs. vanilla.

An easy way to switch is to use an editor that has tab and space-tab support. Convert all your leading space-tabs to tabs, set the tab size to four, and then convert leading tabs back to space-tabs.

Pretty easy to do with a python script too. Just count all the leading spaces, then add the same amount to the beginning of the line and write it back out.

查看更多
登录 后发表回答