[Tutor] Documentation concerns.

Magnus Lyckå magnus@thinkware.se
Thu May 22 22:44:01 2003


At 11:34 2003-05-21 -0600, Bob Gailer wrote:
>So one would expect 'a\tab\tabc\tabcd\t'.expandtabs() to produce 
>'a        ab        abc        adcd        ' where there are 8 spaces 
>between each word?

No. That is not what one would expect, because that's not
what tabs are. It would be pointless to have such a thing
as a tab character if it just meant the same as eight spaces.

>But that's NOT what happens. So the definition is not explicit enough.

A Python manual might not be the place to explain what tabs are.
But I suppose that depends entirely on what knowledge you expect the
reader of the manual to have beforehand.

Try typing the tab key at different places in a normal word processor.
You will notice that it will not always move you forward as many
characters or millimeters or what have you. The same happens in a plain
text editor. It depends on what position you start. The tab key (and tab
character) will bring you to the next tab stop, which is at a certain
position along the line, either measured in columns if it's a normal
editor for fixed fonts, or in inches (or cm etc) for a word processor.

Try the code below in your python interpreter.

 >>> a = 'a\tab\tabc\tabcd\t'
 >>> print a
a       ab      abc     abcd
 >>> print a.expandtabs()
a       ab      abc     abcd
 >>>

Both lines with a, ab, abc etc might not look the same in your email
program, depending on how it handles tabs, but it should look the same
in your python interpreter, and in any normal text editor with a tab
setting of 8 if you copy the text to that. If you have a different tab
setting like 4 in your editor, the lines below should match instead:

 >>> print a
a       ab      abc     abcd
 >>> print a.expandtabs(4)
a   ab  abc abcd

Unfortunately, common explanations such as
http://www.webopedia.com/TERM/T/tab_stop.html doesn't exactly
explain tab settings in the context of simple text files. Since
the question pops up, some kind of reference to an explanation of
tabs mighr be in place, even if it has nothing to do with Python
or programming. Tab stop have certainly existed since long before
electronic computers...

I've seen a professional programmer (lously, but paid) use the
tab key in notepad as he prepared a test file that was based on
fixed position text. He was so baffled when his text ended up in
the wrong fields, and his lines were shorter than expected. It
didn't occur to him that pressing tab didn't insert a sequence
of spaces.

It's not at all unreasonable to believe that potential Python
programmers don't understand tabs, and since tabs vs spaces have
such a central role in Python due to the significant indentation,
it's probably a good idea to explain what tabs are anyway. Perhaps
as an appendix. Both "expandtabs" and the descriptions of
indentation in the tutorial could refer to that. Hm...


--
Magnus Lycka (It's really Lyckå), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The shortest path from thought to working program