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:05

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.

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

查看更多
We Are One
3楼-- · 2019-01-03 01:07

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.

查看更多
再贱就再见
4楼-- · 2019-01-03 01:08

I love tabs but it is somehow incompatible with another rule I like: the 80 column limit.

If one chooses 4 spaces tabs and inserts 10 tabs, then there is space left for 40 characters to fulfill the 80 column limit. If another coder prefers 8 spaces tabs, the same line will appear as 120 characters long and will not appear as a valid 80 columns line!

If you want to define a 80 column limit, then you have to choose a length for a tab. In this case having x spaces or a tab of length x does not really make a difference.

Edit: related thread: Maintaining Maximum Line Length When Using Tabs Instead of Spaces?

查看更多
smile是对你的礼貌
5楼-- · 2019-01-03 01:09

Everyone has different preferences on how much code should be indented. Let's say you share code with someone and he or she has different preferences regarding indentation. If the indentations are in tabs, your friend can always just change the tab width in their editor settings. However, if the indentations are in spaces, your friend will actually have to change the source code if he or she want to set it to their preference. Then when you get your friend's changes, you may decide to change it back to your preference. In this case, you will either have to deal with the tedium of changing indentation levels back and forth, or one person must adopt the other's preferences in indentation level. If both you and your friend use tabs, the fact that you have different preferences is a non-issue, as you can each see different indentation levels while the code remains unchanged. That is why, in my opinion, tabs are better than spaces for indentation in all programming languages.

查看更多
smile是对你的礼貌
6楼-- · 2019-01-03 01:09

The problem with using spaces instead of tabs is the file-size becomes so incredibly large.. For example a 500kb space-indented file could be reduced to 200kb when swapping spaces for tabs which is why I always use tabs.

Smaller file-size means faster loading, compiling, execution ( in some cases ), etc..

To me, there is no point to using spaces, but if someone uses an editor which has issues with tabs, then they can replace "\t" with " " or " " or whatever..

查看更多
家丑人穷心不美
7楼-- · 2019-01-03 01:10

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.

查看更多
登录 后发表回答