954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Python Indentation

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.

Regards,
sharky_machine

mattyd
Posting Maven
2,607 posts since Oct 2006
Reputation Points: 105
Solved Threads: 1
 

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.

Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 

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.

regards,
sharky_machine

mattyd
Posting Maven
2,607 posts since Oct 2006
Reputation Points: 105
Solved Threads: 1
 

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!"

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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.

Jeff

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
 

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!

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

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.

Jeff

(with apologies to the real thing)

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You