John DiMarco on Computing (and occasionally other things)
I welcome comments by email to jdd at cs.toronto.edu.

Thu 02 Feb 2017 13:35

Program Source Code Should be Readable by Human Beings By Definition
Version 3 of the Python programming language made a seemingly innocuous change to the Python programming language: no longer could tabs and spaces be mixed for indentation: either tabs must be used exclusively, or spaces. Hence the following is not a valid Python 3 program:

def hello():
	print("Hello")
        print("World")
hello()
If I run it, here's what I get:
% python3 testme.py
  File "testme.py", line 3
    print("World")
                 ^
TabError: inconsistent use of tabs and spaces in indentation
However, the following is a valid Python 3 program:
def hello():
        print("Hello")
        print("World")
hello()
% python3 testme.py
Hello
World
and so is the following:
def hello():
	print("Hello")
	print("World")
hello()
% python3 testme.py
Hello
World
Confused yet?

As you can, or perhaps more to the point, can't see, the problem here is that the first program uses a tab to indent the first print statement, and spaces to indent the second print statement. The second program uses spaces to indent both, and the third program uses tabs to indent both. But because tabs and spaces are both visually represented as whitespace, it is difficult or impossible to visually distinguish between a correct and an incorrect python3 program through inspecting the source code. This breaks the basic definition of source code: human-readable computer instructions.

No doubt the Python 3 designers have good intentions: to help python programmers be consistent about indentation. But to me, it seems unreasonable to have a programming language where syntactically or semantically important distinctions are not clearly visible in the source code.

/it permanent link


Blosxom