Python: Tabs or Spaces
November 18, 2008 4 Comments
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.