views:

2445

answers:

12

I see on Stack Overflow and PEP 8 that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain.

Is there an underlying reason for spaces to be preferred? I would have thought that tabs were far easier to work with.

+25  A: 

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.

Thomas Wouters
It's true that tabs are invisible and people can't agree on the width of tabs. But the same is also true for spaces. When you mix tabs and spaces, things go wrong. But why are you blaming that situation on tabs and not spaces?
Jim
No, the same is not true for spaces. People can agree on the width of spaces.
Rafał Dowgird
Indeed: we're not talking about the width of *indentation*, but how wide the tab *displays*. A space is always the same width, a tab by itself is always a consistent width, but the combination of tabs mixed with spaces is not.
Thomas Wouters
A single space may always be the same width, but indentation with spaces is not always the same width. I fail to see how agreeing to use tabs n spaces wide is any different to agreeing to indent with n spaces.
Jim
Here's why: with tabwidth at 4 spaces, four spaces followed by a tab will be as wide as 2 tabs. With tabwidth at 8 spaces, which Python uses, the same four spaces followed by a tab will only be as wide as *one* tab. Mix the two styles in a block and the code will do the wrong thing.
Thomas Wouters
Yes, I know the problems mixing the two can cause. What I don't understand is why some people blame that on tabs. The problem is mixing them, not tabs in particular. You could solve the problem by replacing tabs with spaces, but you could also solve the problem by replacing spaces with tabs.
Jim
The problem is your editor might use tabs 8 wide, and mine might use 6 wide. That means when we share code things get messed up. The reason everyone uses spaces is because everyone else uses spaces. It may seem arbitrary, and it is. But it's still a very, very good reason.
Harley
Yes, I might use 8-wide tab indentation and you might use 6-width tab indentation. Just like I might use 8 spaces to indent, while you might use 6 spaces to indent. In *both* cases, you have to agree on indentation width. Once more, they are equal, yet tabs get the blame. Why?
Jim
And no, if I use 8-wide tabs and you use 6-wide tabs, and we share code, it doesn't get messed up. It's all just a single tab to the Python interpreter.
Jim
Tabs get picked on because it's tabs that people don't see. People see spaces. People in general don't expect tabs. Tabs lose because they show like spaces, and spaces don't show like tabs. It's not something you can change, and it's silly to get worked up about it. Don't use tabs and it'll be fine.
Thomas Wouters
What do you mean by, "people see spaces"? Spaces to indent are just as visible/invisible as tabs to indent. I'm not getting worked up about it, I'm just looking for an explanation that makes sense.
Jim
There isn't an explanation other than "this is what happens". It's not about the theoretical value or absolute preference. It's what happens when you mix tabs and spaces, and which people blame. It's about how people, when they see something indented by tabs, do not see tabs, but see spaces.
Thomas Wouters
So in other words, you're saying "don't use tabs", and then blaming everybody else except you when you realise you can't back that statement up?
Jim
No. I'm explaining why you mustn't use tabs: because it confuses *other people*. And quite frequently yourself and your editor. This is not a matter of opinion, this is a very frequent problem among many people using tabs. Which is why PEP-8, the styleguide for the standard library, forbids tabs.
Thomas Wouters
Have you even *read* PEP-8? It *doesn't* forbid tabs. And don't tell me what confuses me and my editor. You know what, I give up. You are clearly attempting to push your own preference, and then, when you are questioned on it, passing the buck.
Jim
The short comment size requires me to not repeat what I said before, so I can't elaborate every point in every comment. Please *read* what I have *actually stated* in this entire thread, because you clearly misunderstand it all, and resort to ad hominem attacks instead.
Thomas Wouters
read this http://kymair.com/archives/45 - I like tabs, because they allow configuration for personal views and they're easier to type; 1 tab == 1 indention. Maybie there should be a new character, I call spabs :D
Joschua
+2  A: 

The universal problem with tabs is that they can be represented differently in different environment.
In a given editor, a tab might be 8 spaces or it might be 2.
In some editors, you can control this, while in others you can't.

Another issue with tabs is how they are represented in printed output. I believe most printers interpret a tab as 8 spaces.

With spaces, there is no doubt. Everything will line up as the author intended.

Benoit
Another one who has fundamentally misunderstood the tab... get a mechanical typewriter and play with it for a while, really! 1 tab is not equal to 8 spaces! it is equal to _up_to_8_spaces_!otoh: with proportional fonts, tabs are the _only_ way to guarantee alignment.
hop
+4  A: 

The most significant advantage I can tell of spaces over tabs is that a lot of programmers and projects use a set number of columns for the source code, and if someone commits a change with their tabstop set to 2 spaces and the project uses 4 spaces as the tabstop the long lines are going to be too long for other people's editor window. I agree that tabs are easier to work with but I think spaces are easier for collaboration, which is important on a large open source project like Python.

+3  A: 

Since python relies on indentation in order to recognize program structure, a clear way to identify identation is required. This is the reason to pick either spaces or tabs.

However, python also has a strong philosophy of only having one way to do things, therefore there should be an official recommendation for one way to do indentation.

Both spaces and tabs pose unique challenges for an editor to handle as indentation. The handling of tabs themselves is not uniform across editors or even user settings. Since spaces are not configurable, they pose the more logical choice as they guarantee that the outcome will look everywhere the same.

Florian Bösch
+1  A: 

You can have your cake and eat it to. Set your editor to expand tabs into spaces automatically.

(That would be :set expandtab in Vim.)

Rod Daunoravicius
+17  A: 

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.

S.Lott
+13  A: 

The answer is given right there in the PEP. 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: chose 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. (Although, I guess, if the BDFL were a proponent of the use of tabs, he would have ignored that argument ;-)

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 ;-)

hop
+1  A: 

The answer to the question is: PEP-8 wants to make a recommendation and has decided that since spaces are more popular it will strongly recommend spaces over tabs.


Notes on PEP-8

PEP-8 says 'Use 4 spaces per indentation level.'
Its clear that this is the standard recommendation.

'For really old code that you don't want to mess up, you can continue to use 8-space tabs.'
Its clear that there are SOME circumstances when tabs can be used.

'Never mix tabs and spaces.'
This is a clear prohibition of mixing - I think we all agree on this. Python can detect this and often chokes. Using the -tt argument makes this an explicit error.

'The most popular way of indenting Python is with spaces only. The second-most popular way is with tabs only.'
This clearly states that both are used. Just to be ultra-clear: You should still never mix spaces and tabs in same file.

'For new projects, spaces-only are strongly recommended over tabs.'
This is a clear recommendation, and a strong one, but not a prohibition of tabs.


I can't find a good answer to my own question in PEP-8. I use tabs, which I have used historically in other languages. Python accepts source with exclusive use of tabs. That's good enough for me.

I thought I would have a go at working with spaces. In my editor, I configured a file type to use spaces exclusively and so it inserts 4 spaces if I press tab. If I press tab too many times, I have to delete the spaces! Arrgh! Four times as many deletes as tabs! My editor can't tell that I'm using 4 spaces for indents (although AN editor might be able to do this) and obviously insists on deleting the spaces one at a time.

Couldn't Python be told to consider tabs to be n spaces when its reading indentations? If we could agree on 4 spaces per indentation and 4 spaces per tab and allow Python to accept this, then there would be no problems.
We should find win-win solutions to problems.

quamrana
What editor are you using? Most I've used have an option to dedent on backspace (emacs behaves this way for instance), regardless of the implementation of the indenting.
Brian
I'm using TextPad
quamrana
You're right - I don't see an option to dedent on backspace, but you can probably deal with it using shift-tab, or reduce indent (ctrl-shift-i by default) instead.
Brian
I'm just trying PyScripter which seems loads better at using spaces when you press tab and removing them in 4's when you press backspace.
quamrana
+6  A: 

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.

Brian
The alignment is trivial though. I simply use the brackets like a block and indent each arg. Also, in your example you very well can use spaces since you're inside an argument list and you can stack as many spaces in there as you want.
Soviut
@Soviut: If you indent with spaces, the alignment is messed up as soon as it's viewed with a different tab size. The only way to preserve it is to use tabs to the indent level, and then spaces for the rest - ie mix spaces and tabs, which leads to its own problems.
Brian
Yeah, which is why i tend to just use the python convention of block indenting on my arguments anyways. Sure, they may not line up with the open brace, but its still clear which line or command they belong to. JQuery syntax operates on a similar principle.
Soviut
+3  A: 

JWZ says it best:

When [people are] reading code, and when they're done writing new code, they care about how many screen columns by which the code tends to indent when a new scope (or sexpr, or whatever) opens...

...My opinion is that the best way to solve the technical issues is to mandate that the ASCII #9 TAB character never appear in disk files: program your editor to expand TABs to an appropriate number of spaces before writing the lines to disk...

...This assumes that you never use tabs in places where they are actually significant, like in string or character constants, but I never do that: when it matters that it is a tab, I always use '\t' instead.

Mark Cidade
+1  A: 

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.

Soviut
A: 

This seems to be a very deep question so here is a zen-ish anwer

what is the width of 0-wide tab

spaces are preferred over tabs because tab is not a simple data character, it is a data and view combined, tab is a bad design :)

Anurag Uniyal