Using tabs instead of spaces in IDLE?

Tim Peters tim.one at home.com
Sun Feb 17 03:21:55 EST 2002


[Tom Bryan, on fiddling the the Tk text widgets default tab width]
> I did.  I changed it to 4 and 4.  I toggled tabs on in Idle.  It
> seemed to work on my Linux box.

Are you sure?  Here's what I see, laboriously replacing tabs with spaces by
hand so you'll see the same thing, and using Courier New (a fixed-width
font, just to simplify life).  It's not the initial whitespace that screws
you, so it's quite likely you don't notice at first.  Everywhere you see a
run of whitespace (one or more blanks) here, I hit TAB once followed by the
letter T:

123456789012345678901
    T   T   T   T   T
a   T   T   T   T   T
ab  T   T   T   T   T
abc T   T   T   T   T
abcd T  T   T   T   T
abcde T T   T   T   T
abcdef T T  T   T   T
abcdefg T T T   T   T

No programmer expects that.  Tk "wants to" put the character following the
first hard tab into column 5, no matter how far to the right of column 5 you
are when you hit the tab key for the first time.  Similarly it "wants to"
put the character following a line's second hard tab in column 9, and again
irrespective of where on the line you are when you hit tab the second time.

What programmers expect is:

123456789012345678901
    T   T   T   T   T
a   T   T   T   T   T
ab  T   T   T   T   T
abc T   T   T   T   T
abcd    T   T   T   T   T
abcde   T   T   T   T   T
abcdef  T   T   T   T   T
abcdefg T   T   T   T   T

It's incredibly jarring when you delete characters "on the left side" of
lines containing hard tabs after changing the Tk default, as the widget goes
thru amazing hoops trying to keep seemingly random portions of the remaining
line at fixed distances from the left margin.

> Ick.  So the tabs will depend on the font, etc.?

Not really; IDLE tries to compute the "average width" of a character in the
font then, but that's just to fool the Tk text widget; see the latter's docs
at, e.g.,

    http://tcl.activestate.com/man/tcl8.4/TkCmd/text.htm

You'll find that the only way to specify non-default tabs under the covers
is to give a list of absolute distances from the left margin (in units of
centimeters or inches or points, etc, but not in units of "characters").

> Hm.  PythonWin might be a good option.  The UNIX heads will probably just
> use vi and Emacs, which I think we can handle.  The current "approved"
> coding style for all languages in the group is supposed to be "always
> indent with leading tabs; for alignment purposes, tabs should be
> viewed as 4 spaces; set your visual display of tabs to whatever you
> prefer."

Well, everyone is entitled to live with the consequences of their
self-imposed insanities <wink>.

> I would, but then we've got spaces in some languages and tabs in others.

Most (all?) programmer's editors can be taught to use different rules based
on file extension.  The standard for the Python library is 4-space indents
and no hard tab characters anywhere.  That's what the Python developers have
learned works best.

> ...
> It's stupid, but the whitespace issue is probably going to be one of the
> biggest obtacles to introducing Jython to my co-workers.  I'll be back to
> tell everyone how it goes.

So don't tell them.  Use Jython yourself, and leave them in the dust.
They'll either beg to learn your secret, or you'll be promoted so high so
fast you won't have to endure them anymore <wink>.

let-'em-stew-in-their-own-tabs-ly y'rs  - tim





More information about the Python-list mailing list