views:

631

answers:

13

Hi All.

While coding python I'm using only 2 spaces to indent, sure PEP-8 really recommend to have 4 spaces, but historically for me it's unusual.

So, can anyone convince me to use 4 spaces instead of 2? What pros and cons?

P.S. And finally, what's easy way to convert all existing codebase from 2 spaces to 4 spaces?


P.P.S. PEP-8 Also srictly recommend not using tabs for indention. read here


So, to summarize:

Pros:

  • Have more space to arrange when wraping string more than 80 lines long.
  • Can copy code from snippets and it just works.

Cons:

  • With deeper level of nested statements you have less space for actual code.

Thanks.

+3  A: 

If you're the only coder working on your source file and there are no coding standards that enforce a particular style, use whatever you're comfortable with. Personally (and in line with our coding standard), I use hard tabs so that whoever is looking at the code can use their own preference.

To make a change, you simply need to change all start-of-line spaces to ones that are twice as large. There are many ways to do this; in the Vim text editor, I can think of two: firstly:

:%s/^\(\s\{2}\)\+/\=repeat(' ', len(submatch(0))*2)

This is a simple regular expression that looks for one or more pairs of spaces at the start of the line and replaces them with twice as many spaces as were found. It can be extended to do all files by opening vim with:

vim *.py

(or the equivalent), followed by (untested):

:argdo %s/^\(\s\{2}\)\+/\=repeat(' ', len(submatch(0))*2)/ | w

Alternatively:

" Switch to hard tabs:
:set noexpandtab
" Set the tab stop to the current setting
:set tabstop=2
" Change all spaces to tabs based on tabstop
:retab!
" Change the tab stop to the new setting
:set tabstop=4
" Go back to soft tabs
:set expandtab
" Replace all the tabs in the current file to spaces
:retab

Of course, many other tools will offer similar features: I would be surprised if something like sed, awk, perl or python couldn't do this very easily.

Al
+1  A: 

Identation and general coding style standards vary from language to language, project to project. There is one reason for adopting a coding style standard: so that the code look uniform, no matter who wrote it. That improves legibility in the project, and, to put it bluntly, it looks better.

There is one reason that is not valid when adopting a coding style standard: because you like it. Coding standards exists precisely because people's preferences vary, and if left to their own, chaos would ensue, to the detriment of all.

If you are writing code for yourself alone, which no one will ever read, go ahead and write it whatever you like. Otherwise, following the accepted standard of your community will make your code much more agreeable to everyone else's eyes. And remember, too, that if you DO decide to contribute code to a community in the future, you'll have an easier time if you are used to their coding style already.

As for changing the tab size, there are are many source code formatters out there which support Python, and most programmer's editors and IDEs also have this capability. You probably have it already, it's just a matter of consulting the documentation for the editor you are using.

Daniel
+8  A: 

There's no "better" indentation. It's a religious holy-war topic. Four is nice because it's enough to make the indentation clear, but not so much that your whole screen is mostly whitespace and you have to scroll horizontally to read half the program.

It also has the upside of being a "half-tab" w/r to the historical definition of a "tab."

Other than that, use whatever your group likes. It's like chocolate vs. vanilla.

An easy way to switch is to use an editor that has tab and space-tab support. Convert all your leading space-tabs to tabs, set the tab size to four, and then convert leading tabs back to space-tabs.

Pretty easy to do with a python script too. Just count all the leading spaces, then add the same amount to the beginning of the line and write it back out.

Christopher
We did the same thing in Emacs. That way the programmers could see the spacing they preferred without forcing it on the rest of the group.
Kelly French
+1  A: 

The PEP isn't the boss of you. If it's already consistently 2-space indented, there's no reason to change all your code to conform to it. You could follow it going forward if you really think it's that vital, but, frankly, I don't. You're better off going with whatever convention provides you (and your coworkers) the most comfort both in reading and writing.

Pesto
+14  A: 

Everyone else uses 4 spaces. That is the only reason to use 4 spaces that I've come across and accepted. In my heart, I still want to use tabs (1 indent character per indent, makes sense, no? Separate indent from other whitespace. I don't care that tabs can be displayed as different widths, that makes no syntactic difference. The worst that can happen is that some of the comments don't line up. The horror!) but I've accepted that since the python community as a whole uses 4 spaces, I use 4 spaces. This way, I can assemble code from snippets others have written, and it all works.

Markus
I think you meant 4 spaces, not 4 tabs ;)
fortran
d'oh. Fixed. :)
Markus
"This way, I can assemble code from snippets others have written, and it all works..." AND it looks and feels as _my_ code :)
xtofl
+1  A: 

I think the real question is why spaces and not tabs... everybody loves tabs! xD

fortran
A: 

using 4 spaces or 2 spaces is entirely up to you. 4 spaces is just a convention. What is most important, don't mix tabs and spaces. Use the space bar

ghostdog74
+1  A: 

Any decent editor (emacs, vim) will abstract this whole nonsense out for you. It will work equally well with spaces or tabs, and it can be configured to use any number of spaces (or any number of space-widths for a tab character). It can also convert between the different formats without too much trouble (see the :retab command in vim).

If you're trying to convert source formatting in bulk, I recommend you take a look at the indent utility.

That said, I can't resist answering the other question... My preference has always been for tabs, since it bypasses the whole issue and everyone can view the source code with the widths set as they see fit. It's also a lot less typing when you're working in editors that aren't helpful with converting it. As far as 2 vs 4 spaces, that's purely cosmetic.

rmeador
A: 

One reason is that if you use less spaces for indentation, you will be able to nest more statements (since line length is normally restricted to 80).

Now I'm pretty sure that some people still disagree on how many nested constructs should be the maximum.

Bastien Léonard
+4  A: 

I like the fact that four space characters nicely indents the inner code of a function, because def + one space makes four characters.

def·foo():
····pass
Ionuț G. Stan
A: 

Also one of reasons is: when you have some long line (longer than 80 symbols) and want to split it in 2 you will have only 1 space to indent, that is a bit confusing:

if code80symbolslong and somelongvariablegoeshere and somelongerthan80symbols \
 and someotherstatementhere:
  # some code inside if block
  pass

if code80symbolslong and somelongvariablegoeshere and somelongerthan80symbols \
  and someotherstatementhere:
    # some code inside if block
    pass
HardQuestions
A: 

If you want to write python code together with other programmers it becomes a problem if you use a different indention as them. Most Python programmers tend to use 4-space indention.

Christian
+1  A: 

It is easier to visually identify long nested code blocks with 4 spaces. Saves time when debugging.

johnwlockwood