Emacs has eaten my python tabs!!!

David Bolen db3l at fitlinxx.com
Mon Jan 27 18:58:39 CET 2003

Sean 'Shaleh' Perry <shalehperry at attbi.com> writes:

> On Sunday 26 January 2003 07:57, Skip Montanaro wrote:
> >     Mongryong> Doesn't python rely on tabs?
> >
> > Nope.  It relies on indentation.  I believe TAB characters advance the
> > indentation to the next multiple of eight columns.
> >
> > Skip
> more explicitly it relies on whitespace characters.  2 spaces, one
> tab, it does not matter as long as each block is consistent in the
> type and amount of whitespace used.

Well, if we're talking explicit (:-)), then it certainly matters to
how the tokenizer internally computes indentation.  It evaluates tabs
as being representative of some number of spaces, and is always
computing the current column depth of the source - it then emits
INDENT and DEDENT tokens which is what the interpreter really
executes code based on.

It's true that as long as you stick strictly to tabs or spaces within
a given file, that being consistent should be sufficient for the
interpreter to "see" blocks the same way you do in any editor, and you
don't really need to know how the text is being parsed.  But if you
ever end up with a mixed file, it's important to know how the
interpreter treats tabs or else even if a single block is consistent,
you might not know how the intepreter treats that block in relation to
other blocks.

Internally, the Python tokenizer will always translate a tab as if it
was the appropriate amount of space characters to indent to the next
8-column boundary.  See Chapter 2's indentation section in the
language reference. (*)

Knowing the internal tab size is important since mixed text (tabs and
spaces) may appear to line up visually in an editor, but if that
editor is configured to something other than 8-column tabs, it may not
match what the interpreter will "see" when parsing the same file.

Of course, as multiple people have pointed out, the best way to avoid
the confusion is to just stay away from mixed tab/space files.

(*) As also noted in this thread, an attempt to detect some editor
comments in a file indicating tab size (and overriding the interpreter
default of 8 character columns) does exist in the tokenizer - it
understood vi from at least 1.5.2, and vim/emacs from 1.6 onwards.

-- David
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \

More information about the Python-list mailing list