Python Indentation, Tabs and PEP8

Python Indentation

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.

From the PEP8 Style Guide;

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.

PEP8 – Style Guide

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.

Read More

Eclipse Themes – Color Theme Plugin

Whether you are an avid long term user or a new user of Eclipse I have no doubt you have contemplated changing the colour scheme. If you’re *nix user especially I’m sure you’ll have had a small panic attack at the horrendous white background. If you’re a Python developer you’ll also have probably found the current colouring doesn’t suit that sort of development, well don’t fear there is a simple answer!

If you have a look at http://www.eclipsecolorthemes.org/ you will find a world of themes available, but first you need to install a plugin. You can import the themes through Eclipse vanilla but I have found that not all the settings in a theme take correctly, also you can’t preview or switch between multiple themes, all you are doing is importing new colour settings.

To get these added bonuses you need to install the Color Theme Plugin for eclipse. Installation requires exactly the same steps as needed to install PyDev in this previous tutorial.

Firstly open up Eclipse (I am doing this in Eclipse 3.7) and navigate to the Help menu on the toolbar, from here select “Install New Software…”.

Eclipse Install New Software ScreenIn the top right select Add and enter the following details;
Name – Eclipse Color Theme Plugin
Location – http://eclipse-color-theme.github.com/update

Select the plugin in the list and hit next, make sure to accept any licenses (after reading and agreeing of course) and hit next until it installs.

Once installed just download the themes you want from http://www.eclipsecolorthemes.org/ and then inside Eclipse head to the toolbar and select Window -> Preferences. Inside here navigate to “Color Theme” inside of “Appearance” which is inside of “General”.

My definite recommendation for any Python developers is the Obsidian Python Theme available here; http://www.eclipsecolorthemes.org/?view=theme&id=1027
(Ignore the links down the right, its designed for Python and is very effective)

Once downloaded you can import a theme through the “Color Theme” screen in preferences via the “Import a Theme” button in the bottom right of the window.

Read More