Image of the glider from the Game of Life by John Conway
Skip to content

Python and the Horrendous Tab Character

This thread was on reddit about a month or so ago, but I didn't stumble on it until just recently. Basically, the thread talks about the dreaded tab character, and it's effect on Python code using different editors. I have pasted the items I find most interesting.

It's amazing the philosophy changes that one can have when stumbling upon something of this nature. Moral of the story? Don't use the tab character when editing Python code. Have your editor fill the tab with spaces to keep everything uniform.

You know, the more I see the debate about spaces and tabs, the more puzzled I become that anyone still wants to use tabs. They *only* work nicely, IME, when a source file has only one author making changes, with only one editor. Give the file to another author (who uses a different editor) and boom! Nasty stuff happens, about 80-85% of the time.

... (snip) ...

I cringe every time I see a tab character in source code that I have to edit, because I know indentation issues are going to be ugly. I have to guess what tabstop settings the original author used so that I can exactly mimic his view of the code, otherwise my own additions will look fine on my screen but ugly on his. And nobody ever seems to document what tabstop settings they use! A simple comment in the header of the file would make a lot of difference...

... (snip) ...

I don't like tab characters. I wish they would go away and that everyone would stop using them. They cause me far too many headaches, and I have never found a good use for them. Every time I have to edit a source file with tabs, I have to explicitly think about indentation levels...

... (snip) ...

Lest anyone get me wrong, I am in no way opposed to using the Tab *key* on your keyboard to indent lines. That's a great use for it. My Vim has the "expandtab", "smarttab", and "autoindent" options turned on, which
makes my Tab key practically into a Do-What-I-Mean key. And it always inserts spaces (ASCII 0x20), not tabs (ASCII 0x09). The Tab key is good. But tab characters are bad.

Tab characters have no place in modern source code. Their use is considered harmful and should be strictly avoided.

{ 4 } Comments

  1. Byron Clark using Firefox 2.0.0.1 on Windows XP | February 1, 2007 at 8:10 am | Permalink

    You can also use the '-tt' option when running python to make any appearance of a tab character in otherwise space indented code throw an exception.

  2. matt harrison using Konqueror 3.5 on Unknown O.S. | February 1, 2007 at 9:31 am | Permalink

    Tabs are all over python code in the wild. There are often threads on comp.lang.python discussing the virtues of both tabs and spaces. The python standard (PEP 8) is to use spaces, but the most important thing is that you (and the other developers working on your code) are consistent.
    The annoyance comes in when you code with spaces and start coding on others code with tabs.

  3. Aaron using Debian IceWeasel 2.0.0.1 on Debian GNU/Linux 64 bits | February 1, 2007 at 9:38 am | Permalink

    Byron-

    I wasn't familiar with that. Luckily, the devs here all fill the tab with spaces.

    Matt-

    Exactly. If we are not all on the same page, then we need to be. Luckily, we are consistent. :)

  4. Lonnie Olson using Firefox 2.0.0.1 on Mac OS | February 8, 2007 at 10:04 pm | Permalink

    I have to disagree with you on this point. Mostly just to point out an alternative view.

    Tab characters are superior in two ways.
    1. Technically - Tab characters take up less space in a file (fewer bytes).
    2. Customizability - Any developer can set their editor to display the tabs at their preferred size, while retaining the integrity of the file.

    The only instances where using Tabs can cause a problem are those when a selfish developer chooses to force their opinion of the size of tab by replacing them with spaces.

    Unfortunately there are many selfish developers. That is also the only reason this debate exists at all. I will admit that as long as people choose to be selfish, this tabs vs spaces debate will cause pain for thousands of developers.

    Until we can resolve this selfishness, I understand the need to force standardization based on the majority's decision.

{ 2 } Trackbacks

  1. [...] are various reasons why it’s a good idea to stick with using four spaces instead of a tab character in Python code. I’m not going to rehash that particular flamewar inducing arguement [...]

  2. [...] http://www.python.org/dev/peps/pep-0008/ http://pthree.org/2007/01/31/python-and-the-horrendous-tab-character/ This entry was posted in development, linux, python, soft. Bookmark the permalink. ← How [...]

Post a Comment

Your email is never published nor shared.

Switch to our mobile site