I have always used tabs for indentation when I do Python programming. But then I came across a question here on SO where someone pointed out that most Python programmers use spaces instead of tabs to minimize editor-to-editor mistakes.
How does that make a difference? Are there other reasons why one would use spaces instead of tabs for Python? Or is it simply not true?
Should I switch my editor to insert spaces instead of tabs right away or keep on going like I used to?
The only inconvenience I experience with using spaces instead of tabs is that you cannot easily remove an indentation level; you have to remove four spaces instead of just one tab.
This is PEP 8 as of July 2017:
It seems this statement doesn't leave room for any other choice.
But this isn't solely what PEP 8 tells us, few lines later:
In the above, the first statement expresses a preference for spaces, and the second statement acknowledges the existence of code indented with tabs and this preference for some coders.
So: PEP 8 is tab indentation tolerant. It doesn't tolerate tab and space mixed for indentation though, which, since indentation itself is mandatory, is understandable.
It may be worth mentioning that Google's Python coding style also follows the 4-space rule.
There are other various arguments and justifications in favor of either tabs or 4-space.
If you work in a company which enforce PEP 8, or regularly share your code with others who follow PEP 8, then common sense dictates 4-space. I am (was, maybe) used to tabs from C/C++. But with a properly set IDE, the difference becomes minimal.
I use two space indentation and an editor (kwrite) that inserts spaces instead of tabs when I hit the tab key.
USE AN EDITOR THAT DISPLAYS TAB CHARACTERS (all whitespace, for that matter). You're programming, not writing an article.
I use tabs. There's no room for a one-space error in the tabs (if you can see them). The problem IS that people use different editors, and the only common thing in the world is: tab==indent, as above. Some bloke comes in with the tab key set to the wrong number of spaces or does it manually and makes a mess. TABs and use a real editor. (This isn't just contrary to the PEP, it's about C/C++ and other whitespace-agnostic languages too).
/steps down from soapbox
My main reason for using tabs over spaces is the backspace key. If I'm on a line and I want to backspace-remove an indentation on just that one line I have to hit backspace 4x if it were spaces; whereas, I only need to hit it once if it's a tab.
I will continue to use tabs because—like was stated before—it's easier to convert from tabs to spaces, but not the other way around.
I'm thinking I want to write a simple program that converts code with spaces into code with tabs, because I freaking hate spaces. They drive me up the wall!
Oh! And using the arrow keys to navigate left and right is always a pain in the ass when it's spaces.
UPDATE: Sublime Text 3 now deletes a full soft tab with the backspace key; though, arrow-key navigation is still tedious.
UPDATE: I now use vscode and also wrote a TabSanity extension for it to solve backspace, delete and arrow-key navigation.
I'm primarily a C++ programmer, but sometimes my projects include small amounts of Python. I use tabs to indent my C++ code. This means that I have three options here:
For my projects, I generally go with option 3.