Concerning Python Indentation Syntax:

I have researched this and read some reasoning behind indentation-level enforcement. My question is: does this exist simply to ensure consistency when dealing with nested statements, a precaution against sloppy composition of code?

I am not complaining about this, and actually I somewhat like this, but it seems to always crop up while compiling, improper indentations throughout my code say, for example, if I add a snippet of code such as a RNG just to test it out; I must then stop and format everything beneath the inserted code in order to get a valid run. This is not much different than stopping to fix C++ errors before a valid compile except for those errors never really relate to indentation.

As I said, I do not dislike this feature, this rule of Python, as I pretty much like everything about Python. I just wish to understand (from someone well acquainted with this language) why the enforcement of indentation was decided upon by the designer of Python as a necessary "evil" :twisted: :cheesy: . Just curious.


Recommended Answers

All 6 Replies

Avoiding the endless begin/end or curly braces, and also the end of line semicolons makes code simpler to read and understand. When you use an editor made for Python, indentations become easy to follow. You absolutley have to avoid mixing spaces with tabs in your indents. That's the main reason you have problems with other folks code! I religiously use spaces (4) only, since tab settings vary so much.

Thank-you Ene Uran .

I need to stick to a set amount of spaces (indentation); I think four (4) spaces seems like a good amount.


Thus spoke the Lord: "Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are the devil's apples!"

I think the indent requirements serve two purposes. One you have already mentioned: making blocks more human-readable. The other purpose is pedagogical, remembering that Python began as a teaching language. By having to indent his blocks, the beginner is forced to see code in blocks.


I agree with Jeff!
I have seen other computer language code like C, where the writer did not not use any indentation. Now you get lost in an ocean of { and } and ; which makes large code almost unreadable!

So the real man's computing challenge would appear to be the

Obsucated Python Contest :mrgreen:

Write a program to play chess in as unclear a way possible.


(with apologies to the real thing)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.