Does python care about indents?

Indentation is essential in Python; it replaces curly braces, semi-colons, etc. in C-like syntax.

Consider this code snippet, for example:

def f(x):
    if x == 0:
        print("x is zero")
        print("where am i?")

If it weren't for indentation, we would have no way of indicating what code block the second print statement belongs to. Is it under the if statement, the function f or just by itself in the document?

Mayank Verma

Mayank Verma

DevOps Engineer at Zeta Suite

Published May 27, 2018

Every programmer who's had experience with C/C++/Java (and many more) understands the pain one has to go through with forced Python Indentations!

Now before you scroll down believing this to be one of those juveniles crying over nothing significant, let me tell you that this isn't one of those typical 'Why Python Indentation Sucks' article that thrashes the language developers left and right for introducing an feature, without clearly explaining what's really wrong with it and whether the benefits really outweigh the flaws. This is an article that does the exact opposite of what you proponents of the indentation feature might be expecting. So, let's get to the details without any further ado!

So, what's wrong with indentations?

I believe most of you, who've gone through a Python script with a good number of nested loops already know what I'll be talking about. For the uninitiated, let's go through them anyway:

  1. Difficult to keep track of indentations if the programmer only gives one or two spaces per level of indentation: It's quite known in the Python development community that for good readability, the minimum spaces per level of indentation should be 4, but does everyone practice that? What was supposed to be the strong point of Python is reduced to nothingness.
  2. Nearly impossible to find out the characters used for indentation: So, Python allows mixing of tabs and spaces during indentation. While any sane developer wouldn't dare to enter this uncharted territory, how many sane people are actually there? When the whole point of forced indentation was better readability, did we just sacrifice readability along with maintainability by introducing mixture of spaces and tabs? This doesn't end here, yet. You can actually select your desired number of spaces and tabs for each level of indentation. If there's a consensus on an optimum no of spaces per level of indentation, why wasn't it enforced just like everything else? I may have an explanation. What if we were forced to have exactly 4 spaces and nothing else per level of indentation? Wouldn't that frustrate newbies from even writing code because they'll have to type 4*n times spaces for n level of indentations? What if you miss by even one space? What was supposed to have been a programming language for beginners would have had become a nightmare for them. So, I feel the developers had to compromise. But at what cost?
  3. Copying code is a nightmare: Have a big project to develop and you've found yourself stuck just because copying code that doesn't follow standard indentation will break even what's been working? We've all been through that. Some Python developers follow their 'own standard' that's not the real convention.
  4. Going back to previous level: So, you've just completed your nested inner for loop and now you're left with a few lines of code in the outer loop. When the script file becomes long enough, it's not always possible to correctly find out the previous level of indentation. One misstep and the code that's supposed to be executed in the outer loop is actually executed in the inner loop. Good luck finding out where you went wrong, because Python most probably won't complain.
  5. Everything seems fine, but you still get the notorious indentation error! : You've spent hours writing a Python script at a competition and just when you think everything is right, everything goes wrong. The program is semantically correct and you've triple checked that, but now Python gives you the deadly IndentationError. You've only 5 minutes left, but you still can't find where you've gone wrong with indenting code. Is it justified that you lose out on points just because you've not indented the code the way Python expects?

Do the benefits outweigh the flaws?

As you might have realized by now, they don't. I've heard from many proponents of the feature that the primary purpose of forced indentation was to curb the bad practice of not indenting code altogether. But at what cost? At the cost of cultivating new bad practices like discussed above? I don't think so. Some you might suggest that most popular IDEs take care of indentation, but what about those who'd rather prefer to write on Notepad (Okay, I don't write on that dreaded editor, but let's not forget about the people who do) or gedit, or vim without any plugins?

Hey crying baby! Are you done ranting about it? So what if there're problems? Is there anything we can do without breaking things?

Yes we can. Actually, I've already attempted to do something about it. It's called PyBrace. PyBrace is an extension of the original Python language that adds support for braces! You can write your nested compound statements in the way you expect with other languages with native support for braces. You can check out the project here: https://github.com/mayank-verma048/PyBrace

So an extended version of an existing language, huh? How're we supposed to share our completely non-compliant code with other people?

You only write your code in PyBrace. It doesn't mean you have to share it in PyBrace. PyBrace toolkit provides a utility called pyb, which is a transpiler that converts PyBrace code to vanilla Python script. So, while you can write your code in PyBrace, others can read and work with vanilla Python script thanks to the transpiler.

  • The transpiler maintains a uniform level of indentation throughout the script and it does that for you. So, goodbye indentation errors!
  • Plus, when you want to come out of a level of indentation, you assure yourself you've done that correctly by putting a close brace. That's it. You need not have a heavy IDE to solve indentation problems.
  • Copy pasting in PyBrace is a charm. Because leading whitespace doesn't matter anymore, all you need to worry about is braces, which are visible, unlike whitespace chracters.

So, check out PyBrace right now and save yourself from Indentation hell!

Others also viewed

Explore topics

Do indents matter in Python?

Indentation is a very important concept of Python because without proper indenting the Python code, you will end up seeing IndentationError and the code will not get compiled.

Does Python rely on indentation?

Python, however, uses indentation. A code block (body of a function, loop, etc.) starts with indentation and ends with the first unindented line. The amount of indentation is up to you, but it must be consistent throughout that block.

How does Python handle indentation?

How to solve an indentation error in Python?.
Check for wrong white spaces or tabs. ... .
Be certain that the indentation for a specific block remains the same throughout the code, even if a new block is introduced in the middle. ... .
Go to your code editor settings and enable the option that seeks to display tabs and whitespaces..

What happen if we skip indentation in Python?

Python is a language where the code is arranged through whitespaces. If there is an incorrect indentation, this will result in an error, and the python interpreter will just return an error function. It uses the PEP8 whitespace ethics. There should be 4 whitespaces used between any alternative or iteration.