[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Tabs v.s. spaces (was Re: Programming first steps.)

On Mon, Nov 17, 2003 at 11:13:59PM +0800, Cameron Patrick wrote:
> I believe that tabs aren't a problem with Python so long as they
> really do indent to a multiple of 8 spaces.  Editors which interpret
> tabs differently are broken^W^W can cause problems when editing Python
> code with tabs and spaces mixed though.

This seems to be Python's greatest Achille's Heel as well as one of its
greatest assets.  Working code in Python has a consistent look and feel.
Improper indenting will give a descriptive error that is easy to track
down.  New programmers feel right at home with it, because it isn't all
that significant of a change from psuedo-code outlines.

  1. step 1
    1.1 substep 1
    1.2 substep 2...

In addition, you're not forced to use a program like indent(1) or
astyle(1) to enforce coding style; it's built into the Python

I have a love-hate relationship with the significant whitespace.  I have
always disliked 8 spaces per tab, because it takes up too much screen
real estate on an 80 column display.  Whenever I coded in C, I set my vi
editor to interpret the tabs as 4 spaces.  My mistake in using this was
displayed when I tried to print with a2ps or enscript, when they were
once again interpreted as 8 spaces.  Arg!

I then switched back to using only spaces for indentation, and this
seems to be a consistent approach, but because personal opinion in
coding style seems to be a right of passage amongst C coders, I could
never get anyone to agree with me. ;-)  Even the venerable linux kernel
only accepts tabs, IIRC[1].

Another problem.  Try cut-n-paste in X between code that uses tabs[2].
Sometimes the tabs are not preserved.  Very odd and annoying.

In any case, the documentation about how tabs and spaces are interpreted
in Python are in the Language Reference[3].  Here's the relavent

        First, tabs are replaced (from left to right) by one to eight
        spaces such that the total number of characters up to and
        including the replacement is a multiple of eight (this is
        intended to be the same rule as used by Unix). The total number
        of spaces preceding the first non-blank character then
        determines the line's indentation.

If you continue reading this passage, you will understand why the
authors of Python (namely GvR) choose this.  It's simple to implement
scope via significant whitespace.

So, tabs v.s. spaces isn't really a concern except when mixing the two.
If you use eight spaces for all indentation, it won't matter.  If you
use some other number, it's best to use spaces exclusively.  If you use
tabs exclusively, changing the appearance in your editor may simply be a
configuration option away.  What will I use?  I still haven't decided;
probably tabs/8 spaces. ;-p

1. http://www.linuxjournal.com/article.php?sid=5780
2. http://mail.python.org/pipermail/python-list/2001-December/075764.html
3. http://www.python.org/doc/current/ref/indentation.html

Chad Walstrom <chewie@wookimus.net>           http://www.wookimus.net/
           assert(expired(knowledge)); /* core dump */

Attachment: pgps6tVm4AGQF.pgp
Description: PGP signature

Reply to: