问题描述
我在Stack Overflow和 PEP 8 上看到建议是仅在Python程序中使用空格用于缩进.我能理解需要保持一致的缩进的感觉,而且我感到很痛苦.
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.
推荐答案
答案是在PEP中给出的[ed:此段落已在 2013 ].我引用:
The answer was given right there in the PEP [ed: this passage has been edited out in 2013]. I quote:
您还需要什么其他根本原因?
What other underlying reason do you need?
直言不讳:如第一段所述,还要考虑PEP的范围:
To put it less bluntly: Consider also the scope of the PEP as stated in the very first paragraph:
目的是使在正式python发行版中提供的所有代码保持一致的格式(我希望我们可以同意这是普遍适用的好东西™).
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™).
由于对于单个程序员来说,空格和制表符之间的决定是a)确实是个问题,并且b)可以通过技术手段(编辑,转换脚本等)轻松解决,因此有一种清晰的方法可以结束所有讨论:选择一个.
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: choose one.
Guido是唯一的选择.他甚至不必给出理由,但他仍然通过引用经验数据来做到这一点.
Guido was the one to choose. He didn't even have to give a reason, but he still did by referring to empirical data.
出于所有其他目的,您可以将此PEP作为建议,也可以忽略它-您的选择,团队的选择或领导者的选择.
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.
但是,如果我能给您一个建议:请不要混合使用它们;-) [ed:不再将制表符和空格混用.]
But if I may give you one advice: don't mix'em ;-) [ed: Mixing tabs and spaces is no longer an option.]
这篇关于为什么Python pep-8强烈建议在制表符上使用空格来缩进?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!