Part of Python’s appeal comes from the fact that it’s so human-readable. Languages like assembly, and even C, will often require a lot of thought to properly conceptualize. But Python’s computer code is much closer to actual human language than most other comparable programming languages. Part of this is due to the language’s syntax and abstraction of lower-level concepts. But another aspect stems from the fact that Python’s interpreter tries to force legible code. The way Python tries to not only force indentation, but consistent use of an indent style, makes the resulting code extremely readable. But at the same time, these quirks of the language can make Python a little unintuitive at first. You’ll soon see why consistency with tabs and spaces is so important. And how to fix some of the problems that come up when you don’t.
Indentation in Python
Python indentation is used in a similar way to how many other languages use curly brackets or curly braces. This is one of the reasons why Python code is so easy to read. The syntax forces people away from a messy coding style. Python doesn’t let you jam all of your program logic into a seemingly endless lump of text. Instead, you’re pushed into cleanly separating your program logic into a distinct code block. This largely creates a net positive for code legibility.
The downside to Python’s insistence on clean indentation is that seemingly identical code can end up treated differently by Python’s interpreter. Because while humans look at the visual manifestation of editing markup, the interpreter is looking at the actual bytes stored within a file. The two typically line up. But there is an occasional exception to the rule. And the primary example is tabs and spaces. White space from pressing the tab key and white space from pressing the space key can look identical. But the Python interpreter sees them as different things.
A Closer Look at Tabs and Spaces
The average Python tutorial will typically skim over tabs and spaces, despite their importance to Python. The two options are usually just held out as up to personal preference. And it’s true that someone writing code with tabs or spaces can use the same program logic. The results might even look the same on his computer. But there are very real differences between the two. To the point where the use of indentation is arguably one of the most important parts of learning to write properly Python code. Python’s indentation is part of what defines it as a language. And it’s important to understand the fundamentals rather than just going along with them.
One of the most significant differences between tabs and spaces is that tab length is inherently inconsistent across platforms. A tab doesn’t consistently translate into a specific number of spaces. The way code using tabs looks on your computer might differ dramatically from that of another if they use a different tab size. And this is where the issue of mixing tabs and spaces comes up. Spaces maintain a consistent size over different platforms.
Now imagine if you had code that mixed tabs and spaces. It could look exactly the same on your system if you used the same amount of spaces as were present in your tab format. But all of the indents could become inconsistent when seen on another computer. Try typing out the following code into a standard text editor.
a = 9
b = 5
if a > b:
print(“of the indent test”)
Indent line four by pressing your tab key. And then try to use your space key to match the indention level of line five with four. If you run the resulting code you might see a python taberror complaining about inconsistent indentation. This is the Python interpreter stepping in to help maintain proper code block style.
Avoiding and Fixing Mixed Indentation
It’s generally fairly easy to avoid inconsistent use of tabs and spaces in your Python program. The easiest way to avoid the problem is to use an IDE with solid Python support. For example PyCharm, an IDE devoted to Python, can automatically convert a tab to spaces. Even many text editors will let you do a search and replace for tabs. However, remember that you still need to maintain a consistent indentation level with your whitespace.
If you’re on OSX or Linux then you almost certainly already have a great text editor called vim on your computer which can work with space character problems. Though vim has also been ported to Windows and almost any other operating system you can think of. On the command line simply type vim to start it up, followed by the name of the python file from the earlier example. Now type “:set list” and press enter. This will let you actually see a visual representation of whitespace. You can then use vim to change a block of code into a proper indentation that won’t have any extra spaces. This is done by simply typing out the following into vim.
And if it looks good you can save by typing escape, followed by :w on vim’s input prompt. Then type :q to exit the program. This will usually take care of a syntax error stemming from the lack of proper indentation. But remember that this isn’t a universal panacea for tab and space conflicts. Any automated solution runs the risk of automatically inserting errors. For example, replacing tabs within strings. As such, it’s usually a good idea to make a backup before using any automated solution to spacing indentation.
If this seems like a lot of work to you, then you’re absolutely right. Inconsistent indentation can be a huge problem in Python code. That’s why coding in Python is always best when you’re doing Python right. And that means using a clean code style that uses spaces instead of tabs for indentation. Again though, keep in mind that most Python IDEs handle this automatically. When you press the tab key in PyCharm, for example, it actually inserts four spaces rather than a tab. And the same is true for most of the other Python IDEs.