Python: Tabs or Spaces

I never understood the controversy surrounding tabs and spaces in indenting Python code. Lots (and I mean lots) of people do not like using tabs (or so I thought) in Python. And a question I had to ask was: what’s wrong with using tabs? Why not use tabs? Why use spaces?

I have to admit I love using the tab key. It makes indenting so easy. Instead of typing four spaces, I type one tab. I am also a lone developer: my code never sees the light of day on any other developer’s computer. So whatever I do works for me. But that’s not how it should be. I should consider good practices and implement them in my daily habits. So I embarked on a mission to find out once and for all why to use spaces instead of tabs.

Turns out I was looking at it the wrong way. I started off with Python track: coding style guide. It said, “Never, ever, ever use the tab character (ascii 0x9)!” I also saw Python: Myths about Indentation, which said, “it is generally a good idea not to mix tabs and spaces for indentation. If you use tabs only or spaces only, you’re fine.” To be very honest, I did not still get why tabs should not be used.

I finally stumbled (ok, googled) upon OT: Tab characters considered harmful (Was: Emacs has eaten my python tabs!!!). This is when I finally understood the controversy: “Lest anyone get me wrong, I am in no way opposed to using the Tab *key* on your keyboard to indent lines.” OK, so that’s it. One may use the tab key but not the tab character. I was confused between the key and the character. But to use the tab key, you should configure your editor to convert tab key into space characters, not tab character. Then you can continue to use tab keys and still be compliant with the practice of using spaces for indentation.

4 Responses to Python: Tabs or Spaces

  1. hs says:

    To configure vim so that it replaces a tab with spaces (when you press the tab key), check out the vim guide to converting tabs to spaces.

  2. Derek says:

    I googled “python tab spaces better” and landed up here!

    I’ve read the article in question “OT: Tab characters considered harmful (Was: Emacs has eaten my python tabs!!!” and disagree that it made things clear. All I can see from the examples that the poster used was that it was clear that the editors used by the programmers had no way to visually depict the difference between a tab and “x number of spaces”. My response to that is – why the heck are using such an editor; it may be OK for HTML (or Some Other Langauge”) but clearly NOT for Python, where whitespace is such a crucial issue.

    IMIO, the real solution would be to mandate use of “3 whitespaces per indent” in all Python programs and disallow anything else…

  3. Pingback: nano configuration | Andrew Berezovskiy notes

  4. gseattle says:

    All those spaces, vast waste of electrons

%d bloggers like this: