Computer Language Syntax Design: on Python's Indentation vs Nesting

, , …,

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. (⁖ What's Slim Markup?What is YAML?)

note that, indentation can be a isomorphic form of nested brackets (as in lisp). This probably needs no explanation. For examples of syntax isomorphic to lisp's nested parens, see:

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"

however, 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.

blog comments powered by Disqus