Syntax Design: Python's Indentation vs Nesting

By Xah Lee. Date: . Last updated: .

python indentation syntax is a pain in the ass

python indentation syntax pain 2018-10-11 45679
python indentation syntax pain 2018-10-11

many programers, began to love Python's indentation after getting used to it. For example, there are several light-weight markup formats based on it invented recently, especially in the Ruby community. 〔see What is Slim Markup?〕 〔see What is YAML?

note that, indentation can be a isomorphic form of nested brackets such as lisp, or nested syntax such as XML. 〔see Nested Syntax: XML vs LISP

however, many don't understand is that, Python, and Haskell too, their indentation is nothing isomorphic to nested brackets, because python's indentation is irregular.

for example, here's a typical Python code:

x = 1
if x == 1:
    print "yes"

in true indentation form isomorphic to nested brackets, it should be this:

=
 x
 1
if
 ==
  x
  1
 print
  x yes

you see, it's not readable, and it's not scalable. The point here is, Python's indentation is ad hoc. There is no simple syntactic rule on which {operator, statement, expression} can or must be followed with newline and indentation. You have to learn by rote. (try to write down python's indentation rules, and you'll ends up with pages of ad hoc descriptions.)

this is in contrast to syntax that are isomorphic to lisp's sexp, such as Mathematica's FullForm, or Scheme Lisp's Indentation Based SRFI-49, or XML.

what this means is that, Python's indentation, cannot be automated. It is, not unlike C, Java syntax of ad hoc ness for every syntax unit of the lang.

the consequence of this is complexity and syntax-induced semantic weakness. See:

but why human didn't have problem learning it? because, you just get used to it. Just as Chinese chars is natural for those who grew up in China.