Coding Style

Indentation In Python

...it is not *that* bad

Python is awesome. Period.

Ok, nothing beats the feeling of security that strongly typed, statically compiled languages like C++ provide you with. But for some quick scripting, Python is my tool of choice. Python allows for some Lisp-like elegance. And the ease of network programming using twisted’s inline callbacks is almost as hard to believe as it is hard to understand how and why it works.

xkcd crowd sourcing
comic by XKCD

But not everybody may agree. In fact, some will reject Python on the basis of what is probably its most controversial language feature: indentation. White space with a meaning. Semantic white space has a few consequences:

  1. Editing becomes a bit more awkward. In contrast to other languages, your editor can not infer the indentation of your current line of code from the context in all cases. This is the largest drawback by far. It slows us down and makes copy&paste more error prone.

  2. It is harder to produce auto-generated code in Python. In C++ or bash any program essentially can be written as a oneliner. Be it a “do this for every file” or a 3D graphic adventure. Nevertheless, auto-generating Python code (maybe using C++? ;-) ) is an odd use case.

  3. It simplifies coding style. And after two downers, this is where things get interesting. It complies with two principles stated in the Zen of Python:

Simple is better than complex.
There should be one– and preferably only one –obvious way to do it.

Overall, inconsistent coding style is nuisance. Specifically, in a language like C++ which comes with quite some technical noise. Here, clean code (in terms of coding style) can help you to decipher foreign parts of the code base much quicker, yet it often does not come for free (even if a well-configured code formater is used). In my eyes, forcing indentation on the programmer at least relieves us from defining a style for code blocks. Consider the alternatives:

// format A 
void function( int i )
    { if(i > 0)  return i + 1; else return i - 1; }
    
//  format B
void function(int i){
    if(i > 0){
        return i + 1;
    } // or should the else be here? 
    else {
        return i - 1;
    }
}

// format C
// while the following line break does not apply to it, Python often
// does not allow new lines unless the escaping is used
void
function (int i)
{
    if(i > 0)
        return i + 1;
    else 
        return i - 1;
}

The problem is a similar one for languages (e.g., Bash) that use ALGOL-style code block delimiters. The if-then-fi and while-do-done lead to even more clutter:

function func {
    if [ $1 -gt 0 ]; then 
        echo $(($1 + 1))
    else
        echo $(($1 - 1))
    fi
}

Let us compare it to Python, which has one and only one simple way to write the code above (and even strips the code from the easily forgotten semi-colons):

def function(i):
    if i > 0: 
        return i + 1
    else 
        return i - 1
        
# also consider the ternary equivalent
def function(i):
    return i + 1 if i > 0 else i - 1

Delicious.

So, will indentation ever be optional in Python? Could we get a dialect with braces? Let’s ask Python ourselves:

 >>> from __future__ import braces ; 
  File "", line 1
SyntaxError: not a chance</pre>

Post a Comment

All comments are held for moderation; Markdown formatting accepted;
By posting you agree to the following privacy policy.

This is a honeypot form. Do not use this form unless you want to get your IP address blacklisted. Use the second form below for comments.
Name: (required)
E-mail: (required, not published)
Website: (optional)
Name: (required)
E-mail: (required, not published)
Website: (optional)