Spaces are the preferred indentation method. Tabs should be used solely to remain consistent with code that is already indented with tabs. Python 3 disallows mixing the use of tabs and spaces for indentation. Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.
The primary focus of PEP 8 is to improve the readability and consistency of Python code. PEP stands for Python Enhancement Proposal, and there are several of them. A PEP is a document that describes new features proposed for Python and documents aspects of Python, like design and style, for the community.
Their research found that spaces were far better for a number of different reasons. Not only is this technique more visually appealing, it allows programmers to make more money. The analysis performed by the team at Stack Overflow found that programmers who use spaces instead of tabs are making more money.
Python uses indentation to highlight the blocks of code. Whitespace is used for indentation in Python. All statements with the same distance to the right belong to the same block of code. If a block has to be more deeply nested, it is simply indented further to the right.
Well well, seems like everybody is strongly biased towards spaces. I use tabs exclusively. I know very well why.
Tabs are actually a cool invention, that came after spaces. It allows you to indent without pushing space millions of times or using a fake tab (that produces spaces).
I really don't get why everybody is discriminating the use of tabs. It is very much like old people discriminating younger people for choosing a newer more efficient technology and complaining that pulse dialing works on every phone, not just on these fancy new ones. "Tone dialing doesn't work on every phone, that's why it is wrong".
Your editor cannot handle tabs properly? Well, get a modern editor. Might be darn time, we are now in the 21st century and the time when an editor was a high tech complicated piece of software is long past. We have now tons and tons of editors to choose from, all of them that support tabs just fine. Also, you can define how much a tab should be, a thing that you cannot do with spaces. Cannot see tabs? What is that for an argument? Well, you cannot see spaces neither!
May I be so bold to suggest to get a better editor? One of these high tech ones, that were released some 10 years ago already, that display invisible characters? (sarcasm off)
Using spaces causes a lot more deleting and formatting work. That is why (and all other people that know this and agree with me) use tabs for Python.
Mixing tabs and spaces is a no-no and no argument about that. That is a mess and can never work.
The answer was given right there in the PEP [ed: this passage has been edited out in 2013]. I quote:
The most popular way of indenting Python is with spaces only.
What other underlying reason do you need?
To put it less bluntly: Consider also the scope of the PEP as stated in the very first paragraph:
This document gives coding conventions for the Python code comprising the standard library in the main Python distribution.
The intention is to make all code that goes in the official python distribution consistently formatted (I hope we can agree that this is universally a Good Thing™).
Since the decision between spaces and tabs for an individual programmer is a) really a matter of taste and b) easily dealt with by technical means (editors, conversion scripts, etc.), there is a clear way to end all discussion: choose one.
Guido was the one to choose. He didn't even have to give a reason, but he still did by referring to empirical data.
For all other purposes you can either take this PEP as a recommendation, or you can ignore it -- your choice, or your team's, or your team leaders.
But if I may give you one advice: don't mix'em ;-) [ed: Mixing tabs and spaces is no longer an option.]
I personally don't agree with spaces over tabs. To me, tabs are a document layout character/mechanism while spaces are for content or delineation between commands in the case of code.
I have to agree with Jim's comments that tabs aren't really the issue, it is people and how they want to mix tabs and spaces.
That said, I've forced myself to use spaces for the sake of convention. I value consistency over personal preference.
The reason for spaces is that tabs are optional. Spaces are the actual lowest-common denominator in punctuation.
Every decent text editor has a "replace tabs with spaces" and many people use this. But not always.
While some text editors might replace a run of spaces with a tab, this is really rare.
Bottom Line. You can't go wrong with spaces. You might go wrong with tabs. So don't use tabs and reduce the risk of mistakes.
The problem with tabs is that they are invisible, and people can never agree on the width of tabs. When you mix tabs and spaces, and you set tabstops at something other than Python (which uses tabstops every 8 spaces) you will be seeing the code in a different layout than Python sees it. And because the layout determines blocks, you will be seeing different logic. It leads to subtle bugs.
If you insist on defying PEP 8 and using tabs -- or worse, mixing tabs and spaces -- at least always run python with the '-tt' argument, which makes inconsistent indentation (sometimes a tab, sometimes a space for the same indentation level) an error. Also, if possible, set your editor to display tabs differently. But really, the best approach is not to use tabs, period.
The main problems with indentation occur when you mix tabs and spaces. Obviously this doesn't tell you which you should choose, but it is a good reason to to recommend one, even if you pick it by flipping a coin.
However, IMHO there are a few minor reasons to favour spaces over tabs:
Different tools. Sometimes code gets displayed outside of a programmer's editor. Eg. posted to a newsgroup or forum. Spaces generally do better than tabs here - everywhere spaces would get mangled, tabs do as well, but not vice-versa.
Programmers see the source differently. This is deeply subjective - its either the main benefit of tabs, or a reason to avoid them depending on which side you're on. On the plus side, developers can view the source with their preferred indentation, so a developer preferring 2-space indent can work with an 8-space developer on the same source and still see it as they like. The downside is that there are repercussions to this - some people like 8-space because it gives very visible feedback that they're too deeply nested - they may see code checked in by the 2-indenter constantly wrapping in their editor. Having every developer see the code the same way leads to more consistency wrt line lengths, and other matters too.
Continued line indentation. Sometimes you want to indent a line to indicate it is carried from the previous one. eg.
def foo():
x = some_function_with_lots_of_args(foo, bar, baz,
xyzzy, blah)
If using tabs, theres no way to align this for people using different tabstops in their editor without mixing spaces and tabs. This effectively kills the above benefit.
Obviously though, this is a deeply religious issue, which programming is plagued with. The most important issue is that we should choose one - even if thats not the one you favour. Sometimes I think that the biggest advantage of significant indentation is that at least we're spared brace placement flamewars.
Also worth reading is this article by Jamie Zawinski on the issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With