Moving from C to Python, wrapping my head around Python indentation was the hardest aspect. Not because they are inherently hard to understand, thats just silly, its because I was so used to having free reign to format my code as I wished.
Python is far stricter and this can arguably be for the better. Following a strict style guideline allows for consistency between projects and files and also improves the readability of your code for now and future maintenance.
So what is the indentation Python likes? Well, you can honestly use any size indentation you wish as long as you are consistent. That means that within a function each declaration or function call must be indented by the same number of spaces, as you expand out into the contents of IF statements then you apply the same indentation again.
Obviously the point of this is to maintain consistency and readability and so there is a recommendation via the style guide of Python, PEP8. This style guide sets down recommendations on how to format and arrange your code. You could choose to ignore it, you could comply with it 100%. Generally you will find a great many developers follow most of its principles but digress on some aspects when using external C scripts or API’s as they can sometimes conflict.
PEP8 recommends a 4 space indentation for your code. It is important to remember that a tab that looks as long as 4 spaces is not 4 spaces (unless your IDE is configured to do so). Generally an editor will throw a tab character in when you use tab, this is bad as the Python interpreter will interpret these differently to spaced indentations, if you use all tabs then the script will operate fine as technically the indentation is the same throughout. If you mix tabs and white space it will end badly as the interpreter will report there is a change in indentation.
Use 4 spaces per indentation level.
For really old code that you don’t want to mess up, you can continue to use 8-space tabs.
Continuation lines should align wrapped elements either vertically using Python’s implicit line joining inside parentheses, brackets and braces, or using a hanging indent. When using a hanging indent the following considerations should be applied; there should be no arguments on the first line and further indentation should be used to clearly distinguish itself as a continuation line.
Eclipse Tab Length
If you are using Eclipse for developing in Python with PyDev then it has a nice easy way you can configure your indentation, navigate to “Window” in the toolbar and select “Preferences”. From here navigate to “PyDev” on the left of the window and then to “Editor” inside the “PyDev” group. Here you can see that you can define the “tab length” and you can also explicitly specify that eclipse replaces tabs with spaces, this is great if you are accustomed to using tab in development indentation as it will just replace your tab with 4 spaces – meeting the PEP8 guidelines.
So what is PEP8? You’ve no doubt seen it referenced in places when working with Python or searching for answers on the internet. PEP8 is basically just a set of guidelines to provide a consistent coding conventions for all Python developers. As I alluded to above, not all developers follow this religiously but it is certainly great to start using when you are starting out.
Another advantage of trying to stick to this when getting started is that you can bolt code analyzers analyzers onto your IDE (Or at least I know you can do this in Eclipse) and PEP8 compliance checkers so your code is checked as you move along, both helping you to learn what mistakes you are making via tooltips, but also allowing you to fix them as you move along.
I will cover how to use code/PEP8 analyzers within Eclipse in a later post as I think the ability to do this is often overlooked but can be a powerful tool to help you learn best practices early on and avoid the pitfalls of common mistakes rising from applying a poor format to your code.