Why Do Indents Make Deep Impressions?

On how to use indents in Python

Programs generally get written in blocks of code. A lot of programming languages use braces to indicate where a block starts and stops. Python doesn’t. It uses indents, sort of like paragraphs in English.  It’s more elegant than brackets, but if you don’t get the indents right, Python will torment you in a way your fifth-grade English teacher never could.

Let’s start with something simple.

Here’s a quick example of how indents can make a difference.

def indentsReallyMeanSomething():                       #Line 1
    print('Which line should print first?')             #Line 2
    print('This line or that line?')                    #Line 3
indentsReallyMeanSomething()                            #Line 4
print('It depends-how do you want to indent your code?')#Line 5

If we leave the indents as they look above, then we get a print out like this:

Which line should print first?
This line or that line?
It depends-how do you want to indent your code?

Now, we can take the indent out of Line 3 so the code looks like this:

def indentsReallyMeanSomething():                       #Line 1
    print('Which line should print first?')             #Line 2
print('This line or that line?')                        #Line 3
indentsReallyMeanSomething()                            #Line 4
print('It depends-how do you want to indent your code?')#Line 5

Then we get a print out like this:

This line or that line?
Which line should print first?
It depends-how do you want to indent your code?

I’ve set this code up so it works fairly well either way, but that’s not usually going to be the case. You might really screw things up by writing code that puts the wrong line first. Imagine telling a customer to check out before he or she even buys your product (doh!), or imagine telling someone to drive left and then right when you really mean to tell them to drive right and then left (WTF?).  Bottom line: the beauty of indents is more than skin deep when it comes to Python.

As a beginner, you should know a couple of more things about indents. First, the huge role that indents play in Python is pretty unusual in the world of programming languages. A lot of Pythonistas argue that it’s a graceful way of telling the computer which coding statements you want grouped together, a process sometimes referred to as “scoping.”

Not everybody is enamored with indentation, however. In fact, it’s one of the more controversial aspects of the language.  Andy Buckley, for example, wrote a blog post in 2007 called “Python indentation considered boneheaded.” He argues, among other things, that:

If more than one person, with different editor settings with regard to tab/space indentation edits the same Python code, a mix of spaces and tabs is pretty much guaranteed. In any other language, this doesn’t affect the bvehaviour of the code: in Python the invisible markup can completely change the logic. Oops.

Such complaints are one reason for the Python Style Guide. I should note that indenting with four spaces is the preferred indentation method in Python. Python 3 actually disallows mixing tabs and spaces for indentation in one piece of code.

As a beginner, I love the look of Python code, which has to be presented well in order to work. On the other hand, I’ll tell you that getting indents right has been one of the hardest things for me to learn.

But, to be honest, correctly using brackets might have proven just as hard. My issues probably have much more with my own boneheadedness than the boneheadedness of the language.

Published by

Mark R. Vickers

I am a writer, analyst, futurist and researcher. I've spent most of my working life as an editor and manager for research organizations focusing on social, business, technology, HR and management trends. But, perhaps more to the point for this blog, I'm curious about the universe and the myriad, often mysterious relationships therein.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s