Python’s Significant Whitespace Problems

Significant whitespace is often touted as a major advantage of Python, but in reality it is a disadvantage and solves no problem that could not be solved in better ways.

Erik Engheim
May 22 · 5 min read
Generated using Carbon with SynthWave ’84 theme

I never wanted to write this story, because I hate it when people get hung up on minor details in a language. In the Julia community we sure have a fair share of them. I don’t know how many times people start ranting about how 1-based indexing is a total deal breaker.

So let me get this out of the way first: Significant whitespace is not a Python deal breaker. You should not avoid using Python because of the use of significant whitespace. So why am I writing this article then if I am fine with it?

Because Python developers keep insisting it is a major advantage, and because many seem unaware of the downsides. I have already written about why I think curly-braces are bad, so what not significant whitespace?

Read more: The Case Against Curly Braces {} Languages.

The Problem Significant Whitespace is Supposed to Solve

In C/C++ it is a classic mistake to write code like this:

// C/C++
while
(i < N) {
if (bar[i] >= y)
print(bar[i]);
count += 1; // oops
}

In this case you may have written the code initially like below, and avoided using the curly braces because it was unnecessary.

// C/C++
while
(i < N) {
if (bar[i] >= y)
count += 1;
}

Thus by accident you have caused count to be incremented on every iteration rather than only when bar[i] >= y. The problem here is that we get tricked by the discrepancy between the visual indentation, which we humans read, and the curly braces that the compiler cares about. The creator of Python, then decided: Let us make how the compiler reads code blocks and how humans read them the same!

# Python
while
i < N:
if bar[i] >= y:
print(bar[i])
count += 1

Except there are plenty of other much simpler solutions to this problem. For example, in the Go programming language you would have to write:

// Go
while
i < N {
if bar[i] >= y {
count += 1
}
}

Curly braces for code blocks are required even for single statement unlike C/C++. Thus you can never make the classic C/C++ mistake. My go-to language, Julia, is the same but with begin-end blocks.

# Julia
while
i < N
if bar[i] >= y
count += 1
end
end

Problems Caused By Use of Significant Whitespace

Okay, so significant whitespace may not solve any problems, but what is the harm?

Say I am experimenting with developing a factorial function, as seen below:

def fac(x: int):
prod = 1
while x > 1:
prod *= x
x -= 1
return prod

In my Python REPL I want to test the factorial of 5 and have written:

>>> x = 5

Next I want to copy paste the code section shown below:

    prod = 1
while x > 1:
prod *= x
x -= 1

This will blow up quickly, because this code segment has whitespace indentation:

Other dynamic languages with a REPL don’t have this problem. Copy-pasting arbitrary text tends to be easy and straightforward. This is what would happen if I tried to copy similar code into my Julia REPL:

For anyone doing functional programming, closures are very useful in many circumstances. In JavaScript, you could write code like this to handle cleanup, such as closing a file after it has been used:

// JavaScript
openfile("foobar.txt", function(file) {
read_data(file)
...
});

You find many similar variants in may different languages from Lua, Julia, Ruby to Swift. But in Python this is not possible. Consider this:

openfile("foobar.txt", def (file):
read_data(file)
...

This could not possibly work in Python because the compiler/interpreter cannot determine the nesting. For example, should double indentation indicate deeper nesting? Either way you end up with hard-to-read code. This is why Python has to invent unique solutions such as with-as, which you don’t find in other languages.

You can use lambda functions in Python, but those are of limited application as they only work for one-liners:

openfile("foobar.txt", lambda file: read_data(file))

More detailed treatment here: Python’s “with as” Statement in Julia, JavaScript, Lua and Other Languages.

If you copy-paste code from a project which uses different indentation than you, then you can imagine you are in for a lot of problems. With other languages this is not a problem. You copy-paste code in with the the begin and end markers. Any formatting tool you may be running will be able to adjust indentation to whatever is used in your project. However with Python you deprive your formatter of that info. What constitutes a block of code is also part of the formatting. You are essentially mixing two entirely separate concerns. Semantics and formatting should not be the same.

Imagine if the meaning of your code changed with the font you used or the coloring you used on your keywords? Such stylistic concerns should be separate from meaning of your code.

With modern IDEs automatically running code formatting tools even the old C problem would be gone because the formatter would remove the indentation where there is no actual code block.

Readability and Code Length

The only benefit I can see from significant whitespace is that you shorten the line count by removing what marks the end of a code block. However I think that is a double-edged sword. The end of a block becomes in my opinion less clear. When I scan code, I find it much easier to notice the end of a block of code when keywords are used as in this Julia example matching the Python cover image to this story.

There is a certain symmetry here when you read the code. Each block of code ends with some new keyword such as elseif, else or end. However I accept that this is likely something that is heavily influenced by what you read daily. The important point however is that I cannot see how removing the end markers improves anything. Sure the code gets shorter, but is that worth it?

Star Gazers

“If you want to master something, teach it.”

Erik Engheim

Written by

Geek dad, living in Oslo, Norway with passion for UX, Julia programming, science, teaching, reading and writing.

Star Gazers

The place that enthusiastic and stargazer to everything.

Erik Engheim

Written by

Geek dad, living in Oslo, Norway with passion for UX, Julia programming, science, teaching, reading and writing.

Star Gazers

The place that enthusiastic and stargazer to everything.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store