Python: using 4 spaces for indentation. Why? [closed] Python: using 4 spaces for indentation. Why? [closed] python python

Python: using 4 spaces for indentation. Why? [closed]


Everyone else uses 4 spaces. That is the only reason to use 4 spaces that I've come across and accepted. In my heart, I still want to use tabs (1 indent character per indent, makes sense, no? Separate indent from other whitespace. I don't care that tabs can be displayed as different widths, that makes no syntactic difference. The worst that can happen is that some of the comments don't line up. The horror!) but I've accepted that since the python community as a whole uses 4 spaces, I use 4 spaces. This way, I can assemble code from snippets others have written, and it all works.


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


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