Python IndentationError

Python is the first programming language I encounter where code indentation is more than a cosmetic issue or a means to make code more readable. In Python code indentation  actually replaces the “DO-END”s or curly brackets used in other languages to define code blocks, for example after an if-statement.

What looks like an elegant way to type less code introduces a new type of error I never have seen in any other programming language: the IndentationError, like this one: IndentationError: unexpected indent

Python requires one level of indentation to be four space characters ! Usually when using a text editor I ( and probably most programmers ) perform indentation conveniently by using the TAB key. Nevertheless, a tabulate character is not necessarily the same than four space characters, as a fact it is not.

When writing my first little Python program using my favorite code editor Notepad++ I was safe as long as I did not use more than one line of code in a code block. As soon as I started to use larger code blocks I suddenly bumped into this error and had to learn that lesson: a tab character is not equivalent to four space characters. Unless you tell your editor to convert a tab character to four space character, like it can be done in Notepad++ for the Python language, under Preferences –> Language Menu/Tab Settings:

After checking on “Replace by space” I seem to have that problem under control. I just wonder why this hasn’t been a default setting.


Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: