Whitespace as syntax (was Re: Python Rocks!)

Gordon McMillan gmcm at hypernet.com
Mon Feb 7 23:05:19 EST 2000


fcahoon at my-deja.com writes:
[skip to example]
> 
> if condition1:
> <S><S><S><S>statement1
> <S><S><S><S>statement2
> <S><S><S><S>if condition2:
> <S><S><S><S><S><S><S><S>statement3
> <T>statement4
> 
> To which loop does statement4 belong?

Unambiguous. It belongs to "if condition2". Python interprets 
tabs as 8.

> This is what I've seen in some C code that's been through many hands:
> 
> The old-timers used 8-space tabs to effect 3-space indentation.  Don't
> ask me why.  

1 indent = 3S, 2 indent = 6S, 3 indent = TS. Python will warn 
about this, because there are interpretations of "tab" that yield 
inconsistent results. Thus most coders use only spaces or 
only tabs.

> The 2nd generation of coders set their tabs to 3 spaces in
> the editor, editing some parts of the code, unaware that other parts of
> the same file contained 8-space tabs.  

This will almost certainly make some line ending in a colon 
appear to be followed by a dedent. That's quite noticable. If the 
2nd programmer edits a block indented by the first 
programmer, Python will give him a syntax error. 

If he adds a brand new block, no problem, because 
indentation is everywhere locally consistent.

> Tabs were converted to spaces
> under the mistaken assumption that they were all 3-space tabs.

There are Python tools that will convert Python code from any 
indentation style to any other style, based on a Python parse 
of the code, not the programmer's guess.

Deformations can proceed indefinitely in C, but they will be 
very quickly noticed in Python, so the code won't stay 
strangely formatted for long.
 
Just as I run indent on all but the most pristine C code, I run 
tabcleaner on any oddly formatted Python.

- Gordon




More information about the Python-list mailing list