Tabs versus spaces in Python programming

2019-01-03 00:44发布

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?

30条回答
神经病院院长
2楼-- · 2019-01-03 01:24

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.

查看更多
SAY GOODBYE
3楼-- · 2019-01-03 01:24

This is PEP 8 as of July 2017:

Enter image description here

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:

Enter image description here

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.

查看更多
迷人小祖宗
4楼-- · 2019-01-03 01:28

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

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-03 01:29

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

查看更多
祖国的老花朵
6楼-- · 2019-01-03 01:29

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.

Tabs vs. Spaces for Indentation

UPDATE: I now use vscode and also wrote a TabSanity extension for it to solve backspace, delete and arrow-key navigation.

TabSanity Extension in Action

查看更多
\"骚年 ilove
7楼-- · 2019-01-03 01:29

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:

  1. Use tabs in C++ and spaces in Python. This allows my C++ files to remain as they are and I follow the PEP-8 recommendation, but I am inconsistent within my project.
  2. Change my C++ code to use spaces. This allows all of my files within my project to be consistent, and I follow the PEP-8 recommendation, but requires me to go back and change all of my C++ files. I consider this a bad thing because I prefer tabs.
  3. Use tabs in my C++ code and Python code. This makes my entire project consistent and allows me to use my preferred indentation style: tabs. The downside is that I am not following the PEP-8 standard.

For my projects, I generally go with option 3.

查看更多
登录 后发表回答