views:

7151

answers:

13

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 different? Are there other reasons why you 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?

+44  A: 

Because PEP-8 tells us to use spaces :)

Alexander Kojevnikov
Well, I guess that is a quite good answer! Hadn't seen that one, thanks!
kigurai
Haha, that was the fastest accept :)
Alexander Kojevnikov
Actually I didn't see this as requiring spaces since the second point was to never mix spaces and tabs - why have that if you can't use tabs.I saw it as ensuring there was a 4-character gap for indent which could be done with 4 spaces or a tab with tabstops set at 4 (that's how I do my Python.
paxdiablo
And then I saw "For new projects, spaces-only are strongly recommended over tabs." - boy, do I look stupid now. Hovever, they're recommendations in a guide so I'm not changing my coding style :-).
paxdiablo
Is that what you call an argument?
Olivier
paxdiablo: Python doesn't know what your tabstop is set at, your file just puts in a tab character and your editor displays it however you tell it. The reason for always using 4 spaces is you can't reliably mix tabs. If you start a function using tabs and then finish with 4 spaces per indent, the interpreter thinks you've ended the function when it hits the transition. Since tabs and spaces are both invisible, this is non-obvious when you first encounter such an error, and you see a message complaining about unexpected indentation while it looks fine in your editor.
bobpaul
+11  A: 

I recently came across an article titled Python: Myths about Indentation which discusses this and related questions. The article has good reasons for recommending the use of spaces when writing Python code, but there is certainly room for disagreement.

I believe it's true that most Python programmers use spaces only.

Greg Hewgill
+18  A: 

The most "pythonic" way is to use 4 spaces per indentation level. The Python interpreter will however recognize spaces or tabs. The only gottcha is you must never mix spaces and tabs, pick one or the other. That said, the specification recommends spaces, most developers use spaces, so unless you have a really good reason not to, I'd say go with spaces.

ctcherry
+2  A: 

Editor-to-editor mistake occurs when you have mixed indentation within a file. This arises as follows: a block of code is indented with 4 spaces, and then one indentation level "in", it is indented with tabs. Now the heathen who did this (mixing tabs and spaces) had it so his tabs are also 4 spaces, so he sees no problems, and python sees no problems. Now our victim comes along later, and he has his tabs set to 8 spaces. Now our victims thinks the code looks all whacked, and fixes it by removing one level of indentation, which now makes the code look like it is still 2 levels of indentation, but is really one level. At this point all hell breaks loose.

The lesson here is that you should never, ever, mix tabs and spaces. If you keep to this, then it is easy to reindent your code into spaces or tabs, regardless of which you personally use. The best way to ensure you don't mix tabs and spaces is to always run python with -tt, which will produce an error when tabs and spaces are mixed.

As for tabs and spaces, I personally use tabs so separate indentation from appearance - it is much easier to change the appearance of code when it is indented with tabs than it is with spaces. I know this runs contrary to what 99% of python programmers do, but that is my personal preference, and it is easy in any case to convert a tabbed file to a spaced one. The reverse is not always true, since you can accidentally whack out 4 spaces in strings etc.

freespace
So, unless mixing tabs and spaces (which I agree is bad) then there is no other reason than convention?
kigurai
Convention covers also the possibility of code exchange.
ΤΖΩΤΖΙΟΥ
+2  A: 

You CAN mix tabs and spaces... BUT a tab is considered to be the same indentation as 8 spaces, so unless your editor is set up to consider a tab to be 8 spaces you're asking for trouble when mixing them.

Ignacio Vazquez-Abrams
A: 

People will use different editors on the same code. These editors will represent a tab on the screen differently. If you're working on an editor that represents a tab as 4 spaces, if you indent the first line by "\t " and the second by "\t\t", they'll look like they're in the same indent level: 8 spaces.

The python interpreter doesn't know your editor, and he has to interpret the tab as some amount of indentation. In fact, it interprets the tab as 8 spaces, so he'll see different indent levels than what you intended: 12 spaces for the first line, 16 spaces for the second. You're toasted.

Rod Daunoravicius
Sounds like bad editor. Which editor does that ? Why doesn't it display those on different positions (1 tab on 4 spaces position, 2 tabs on 8 spaces position) ?
alpav
+4  A: 

Use an editor that allows you to insert spaces up to the tabstop when you press the TAB key, instead of inserting a \t character. And then forget about it.

Rod Daunoravicius
A: 

I use two space indentation and an editor (kwrite) that inserts spaces instead of tabs when I hit the tab key.

David Locke
+2  A: 

The only inconvenience I experience with using spaces instead of tabs is that you cannot easily remove an indentation level, you have to remove 4 spaces instead of just one tab.

Ikke
Many editors have an "unindent" keystroke, such as Shift-Tab or Ctrl-[. Some editors even go so far as to erase 4 spaces when you hit Backspace at the right part of the line.
RJHunter
The problem is that it's 'some' editors. Most editors wont.
Ikke
Most editors that are designed for programming will. If your editor lacks this feature, it's probably missing a lot of other useful functionality.
Steve S
Ikke - what are you using? Vim/GVim certainly do. So does emacs. I believe TextPad and Notepad++ do as well. If your hammer is broken don't blame the nail.
bobpaul
+16  A: 

Tired of chasing after indentation typos ( 8 spaces ? no, 7 oops 9 ... ) I switched my sources to 'tabs only'.

1 tab == 1 indent level, full stop

The point is: if you want to display the indentation as 4, 8 or pi / 12 character width, just change the settings in your text editor, don't mess with the code :p

(personally I use 4 char width tab... but some would prefer 3 or 8 space, or even use variable width fonts !!)

yota
This is my preference too, but I’d love to hear a clear statement of the arguments against it. Is it that some editors don’t let their users decide how wide a tab stop should be? If so, um, hey programmers: **get a better f—ing editor**
Paul D. Waite
+1. I totally agree with the statement above in boldface. 1 tab == 1 indent level.
Olivier
Or to put it another way: "A Tab" is meaningful. "A space" is not. It's meaning is dependent on the standard for that particular file. And considering that Python /relies/ on indentation levels, I tend to go with the one that explicitly indicates such: tabs.
Ipsquiggle
of course, if you're likely to have the off-by-one space indentation problem you speak of, you can still have it with tabs (by accidentally entering a space immediately before a tab). However, when this happens it's impossible to see. At least with all spaces things don't visually line up if you make a mistake. With tabs, a whitespace mistake can be invisible to the naked eye.
Bryan Oakley
That works OK if the only reason you ever need code to be indented is for creating a new scope. What about, say, a newline between grouping ()[] characters? You can (a) limit your code to never use any but the most simple generators, or (b) live with code that has funny indenting all over the place, or (c) mix tabs and spaces. All of these seem much worse to me than simply using 4-space indents.
Ken
@Bryan Oakley: But if you accidentally somehow insert a tab into your spaces the same will be true - the indentation will still be messed-up and you won't be able to see it. I think when indentation is as important as it is in python you should be able to see your indentation clearly and most editors can show them to you.@Ken: Always use a single tab for any additional indent (like inside brackets) and you're good. I don't approve alignment at all. This is not an ASCII art, it's code!
inkredibl
+1  A: 

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.

sfjedi
If you are using linux, there is a program called `unexpand` which converts spaces to tabs. (`expand` converts tabs to spaces.)
unutbu
that sounds excellent, but alas! i'm not using linux. thanks for the tip though.
sfjedi
A good text editor should do the work for you whatever your preferred choices are.
Roberto Bonvallet
I'm using Notepad++ for Python and it definitely allows me to do some spacing settings, but I still have the arrow-key and backspace navigation issues w/ spaces.
sfjedi
Use Ctrl+Left and Ctrl+Right to navigate faster. It's a keyboard shortcut built into most text editors (as in, everything except Notepad) that moves to the next word. And you should never have to press left/right through tabs, since they're always at the beginning of a line.
Jonathan Sternberg
+1  A: 

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

EmTee
A: 

Use spaces, if your text editor (or your workmates editor) have a different tab length you will start getting lots of errors of "unexpected alignment"

You can even have trouble if you code alone. Just in the very moment you have two lines within the same block but one aligned with tabs and the other with spaces.

Francisco Garcia