Skip to content

to tab or not to tab: whitespace in python

Whitespace in python as seen with the editor 'PSPad'

For a while I’ve been pondering whether I should use spaces or tabs to indent -code.

On WhitespaceInPython I’ve collected some pros and cons and finally decided that tabs are the way to go for me. This decision was based, amongst other arguments, on this comment to A Brief Defense of Significant Whitespace:

Indentation consistency becomes easy if everyone agrees to use tabs. Then, each user, in their editor of choice, needs to decide what their tabstop should be.
If you don’t like huge indentations, set your tabstop to 4, or dare to even try 2.
If you like lots of whitespace, set the tabstop to 8. If your editor can’t do this, get yourself a good editor.

I never really understood why the BDFL feels so strongly about using spaces for indentation:

If it uses two-space indents, it’s corporate code; if it uses four-space indents, it’s open source. (If it uses tabs, I didn’t write it! 🙂[source]

On Friday WhitespaceInPython was edited by someone calling himself ‚thyself‘, adding:

Guns don’t kill people, bullets kill people. And tabs don’t mess up code, editing does. As long as you don’t need to ever change the code, or transfer it, or refer to column numbers, or use different editors, or different environments, or post it on message boards, etc., no problem. In real life though, use consistent spaces to obtain consistent results.

Though bullets are another story (see PowerPoint), and I almost never refer to column numbers (;-)), I see the weight of the points made. Time to switch back to spaces?

Frankly, I currently feel quite comfortable with tabs and use them all over the place. I haven’t run into trouble using them but may switch back to spaces if I finally do…

But feeling that slight uncertainty again, I googled around a bit and came upon that wonderful page Indentation Using Tabs, where I could read:

Like the Newline, Tabs also have special meaning. Using Spaces to get to the next level of indention is no more acceptable than using Spaces to advance the cursor to the beginning of the next visual line. Tabs and Newlines contain metainformation about the code that using an arbitrary amount of spaces to get the same visual effect does not have.

I think I’ll stick with tabs. They just feel more sensible… 😉

{ 5 } Comments

  1. Emerentia | 2006/5/3 at 12:00 | Permalink

    Why „discussing“ about tabs or spaces? Peeking at old-fashioned programming-languages like Cobol or Fortran will give you an old-fashioned but consequential answer: use spaces – the only thing punchcards understand 😉
    Seriously, I would prefer tabs while coding, but would like them to become spaces for saving, ‚cause tabs are what your editor wants them to be…

  2. Steffen | 2006/5/17 at 10:00 | Permalink

    Lighthearted of difficult things you speak, young padawan!

    I am convinced that tabs are the better abstraction for „one level of indentation“. They also save space (no pun intented) – so your punchcards would use tabs if they would at all bother about indentation…

    In the last two weeks I worked with ‚real tabs‘ and ’spaces‘ (convert-tab-to-spaces) and found several points:

    • In comments for commits to SourceSafe or SVN tabs look ugly (and cannot be entered directly). The ugliness (using 8 spaces for one tab) may be due to some setting I could change somewhere…
    • Code copied from web-pages never displays tabs.
    • There is a huge burden of legacy (old, un-modern editors not being capable of processing tabs correctly, etc.)
    • Most people (at least those that write interesting code) use spaces.

    So even though tabs are the better abstraction I’ll have to bend before the masses. That seesaw of converting spaces to tabs back and forth is just draining too much time and energy…

  3. Douglas Jarquin | 2007/6/13 at 02:34 | Permalink

    I recently went through a similar dilemma. I decided on tabs. Just trying to keep it simple.

  4. Idetrorce | 2007/12/15 at 02:31 | Permalink

    very interesting, but I don’t agree with you

  5. Steffen | 2007/12/16 at 07:53 | Permalink

    @Idetrorce: Does this already qualify as an opinion?

Post a Comment

Your email is never published nor shared. Required fields are marked *