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

Python indentation (3 spaces)

On 10/5/2018 4:48 PM, ts9564 at gmail.com wrote:
>     I am new to Python programming but have significant SQL and C experience. My simple question is,"Why not standardize Python indentations to 3 spaces instead of 4 in order to avoid potential programming errors associated with using "TAB" instead of 4 spaces?"
> Thoughts?

You assumption that a tab means '4 spaces' is wrong.  A tab means 'jump 
to the next tab stop'.  On 10 char/inch US typewriters, tab stops were 
initially set to every 5 spaces or 1/2 inch.  In terminals and code 
editors, virtual tab stops were often set to every 8 spaces, and python 
still interprets tabs that way.

This means that editing Python code in an editor that *displays* tabs 
(as opposed to converting them to spaces) with other than every-8-space 
tab stops is arguably a bad idea if the file has or might have non-tab 
space indents.

By making the standard indent 4 spaces, Python already partly did what 
you want.  The following code is a SyntaxError in both 2.7 and 3.x.

if 1:
     a = 1  # 4 space indent
	b = 2  # 1 tab indent, which may or may not display as 8 spaces

In 3.x, the following raises
TabError: inconsistent use of tabs and spaces in indentation

if 1:
     if 2:  # 4 space indent
	b = 2  1 tab indent

In 2.x, this runs, but if you run it from IDLE, you get a message 
suggesting that you change the tab to spaces and telling you how to do 
so.  This is because IDLE runs your code through the tabnanny script, 
which has the check later incorporated in compile in 3.x.  One can also 
just check one's code without running it.

In other words, it is easy to expose indent issues without changing the 
4 space standard.

Terry Jan Reedy