为什么 Python pep-8 强烈建议使用空格而不是制表符进行缩进?

发布于 2024-07-05 03:10:49 字数 1449 浏览 11 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

挽梦忆笙歌 2024-07-12 03:10:51

除了已经提到的所有其他原因(一致性、从不混合空格和制表符等)之外,我相信 4 个空格约定还有更多原因需要注意。 这些仅适用于 Python(也可能适用于缩进有意义的其他语言)。 选项卡在其他语言中可能会更好,具体取决于个人喜好。

  1. 如果编辑器不显示选项卡(根据配置,这种情况在相当多的情况下会发生),另一位作者可能会假设您的代码使用了 4 个空格,因为几乎所有公开可用的 Python 代码都是如此; 如果同一个编辑器恰好有 4 的制表符宽度,则可能会发生令人讨厌的事情 - 至少,那个可怜的人会因为缩进问题而浪费时间,而通过坚持惯例,缩进问题本来可以很容易避免。 因此,对我来说,首要原因是避免一致性错误。

  2. 重新思考制表符和空格哪个更好的问题,我们应该问制表符的优点是什么; 我见过很多赞扬标签的帖子,但很少有令人信服的论据; 像 emacs、vi(m)、kate 等优秀的编辑器会根据代码的语义进行适当的缩进 - 即使没有制表符; 相同的编辑器可以轻松配置为在退格键等上取消缩进。

  3. 有些人在决定代码外观/布局的自由度方面有非常强烈的偏好; 其他人更看重一致性而不是这种自由。 Python 通过规定对块等使用缩进,大大减少了这种自由度。这可能被视为一个错误或一个功能,但它在某种程度上是选择 Python 带来的。 就我个人而言,我喜欢这种一致性——当开始在新项目上编码时,至少布局接近我习惯的,所以它相当容易阅读。 几乎总是如此。

  4. 使用空格进行缩进可以实现“布局技巧”,从而有助于理解代码; PEP8 中列出了其中的一些示例; 例如。

    foo = long_function_name(var_one, var_two, 
                               var_三、var_四) 
    
      # 列表也一样 
      a_long_list = [1, 
                     2、 
                     # ... 
                     79] 
    
      # 或字典 
      a_dict = {"a_key": "a_value", 
                “另一个键”:“另一个值”} 
      

    当然,上面也可以很好地写成

    foo = long_function_name( 
          var_one、var_two、 
          var_三、var_四) 
    
      # 列表也一样 
      a_long_list = [ 
          1、 
          2、 
          # ... 
          79] 
    
      # 或字典 
      a_dict = { 
          "a_key": "a_value", 
          “另一个键”:“另一个值”} 
      

    然而,后者需要更多的代码行,有时人们认为更少的代码行更好(因为你在一个屏幕上获得更多的代码)。 但如果你喜欢对齐,空格(最好有一个好的编辑器的帮助)在某种意义上给你在 Python 中比制表符更多的自由。 [好吧,我猜有些编辑器允许你用制表符做同样的事情;) - 但是使用空格,所有编辑器都会这样做...]

  5. 回到其他人提出的相同论点 - PEP 8 规定(好吧,强烈推荐)空格。 当然,如果进入一个仅使用选项卡的项目,您别无选择。 但由于 PEP 8 约定的建立,几乎所有 Python 程序员都习惯了这种风格。 这使得就大多数程序员接受的风格达成共识变得非常容易……否则让个人就风格达成一致可能会非常困难。

  6. 有助于强化风格的工具通常无需额外努力即可识别 PEP 8。 这不是一个很好的理由,但让事情开箱即用真是太好了。

Besides all the other reasons already named (consistency, never mixing spaces and tabs etc) I believe there are a few more reasons for the 4 spaces convention to note. These only apply to Python (and maybe other languages where indentation has meaning). Tabs may be nicer in other languages, depending on individual preferences.

  1. If an editor doesn't show tabs (which happens, depending on the configuration, in quite a few), another author might assume that your code uses 4 spaces, b/c almost all of the Python code being publicly available does; if that same editor happens to have a tab width of 4, nasty things may happen - at least, that poor person will lose time over an indentation issue that would have been very easy to avoid by sticking to the convention. So for me, the number one reason is to avoid bugs with consistency.

  2. Reframing the question of which is better, tabs or spaces, one should ask which the advantages of tabs are; I've seen plenty posts praising tabs, but few compelling arguments for them; good editors like emacs, vi(m), kate, ... do proper indentation depending on the semantics of your code - even without tabs; the same editors can easily be configured to unindent on backspace etc.

  3. Some people have very strong preferences when it comes to their freedom in deciding the look/ layout of code; others value consistency over this freedom. Python drastically reduces this freedom by dictating that indentation is used for blocks etc. This may be seen as a bug or a feature, but it sort of comes with choosing Python. Personally, I like this consistency - when starting to code on a new project, at least the layout is close to what I'm used to, so it's fairly easy to read. Almost always.

  4. Using spaces for indentation allows "layout tricks" that may facilitate to comprehend code; some examples of these are listed in PEP8; eg.

    foo = long_function_name(var_one, var_two,
                             var_three, var_four)
    
    # the same for lists
    a_long_list = [1,
                   2,
                   # ...
                   79]
    
    # or dictionaries
    a_dict = {"a_key": "a_value",
              "another_key": "another_value"}
    

    Of course, the above can also be written nicely as

    foo = long_function_name(
        var_one, var_two,
        var_three, var_four)
    
    # the same for lists
    a_long_list = [
        1,
        2,
        # ...
        79]
    
    # or dictionaries
    a_dict = {
        "a_key": "a_value",
        "another_key": "another_value"}
    

    However, the latter takes more lines of code and less lines are sometimes argued to be better (b/c you get more on a single screen). But if you like alignment, spaces (preferably assisted by a good editor) give you, in a sense, more freedom in Python than tabs. [Well, I guess some editors allow you to do the same w/ tabs ;) - but with spaces, all of them do...]

  5. Coming back to the same argument that everybody else makes - PEP 8 dictates (ok, strongly recommends) spaces. If coming to a project that uses tabs only, of course, you have little choice. But because of the establishment of the PEP 8 conventions, almost all Python programmers are used to this style. This makes it sooooo much easier to find a consensus on a style that is accepted by most programmers... and having individuals agree on style might be very hard otherwise.

  6. Tools that help enforcing style are usually aware of PEP 8 without extra effort. That's not a great reason, but it's just nice to have things work ~out of the box.

淡淡的优雅 2024-07-12 03:10:51

你可以鱼与熊掌兼得。 设置编辑器自动将制表符扩展为空格。

(这在 Vim 中是 :set Expandtab 。)

You can have your cake and eat it to. Set your editor to expand tabs into spaces automatically.

(That would be :set expandtab in Vim.)

合久必婚 2024-07-12 03:10:51

关于 之间的讨论Jim 和 Thomas Wouters 在评论中。

问题是......既然制表符和空格的宽度都可以变化——并且由于程序员不能就任一宽度达成一致——为什么制表符要承担责任。

我同意吉姆的观点——标签本身并不是邪恶的。 但有一个问题......

通过空格,我可以控制“我自己的代码”在世界上每个编辑器中的外观。 如果我使用 4 个空格——那么无论您在什么编辑器中打开我的代码,它与左边距的距离都是相同的。 对于制表符,我受制于编辑器的制表符宽度设置的支配——即使对于我自己的代码也是如此。 我不喜欢这样。

因此,虽然空格确实不能保证一致性,但它们至少可以让您更好地控制自己代码的外观,而制表符则无法做到这一点。

我认为,空格使实现(和强加)更容易,这并不是程序员编写代码的一致性,而是编辑器显示代码的一致性。

On the discussion between Jim and Thomas Wouters in the comments.

The issue was... since the width of tabs and spaces both can vary -- and since programmers can't agree on either width -- why is it that tabs bear the blame.

I agree with Jim on that -- tabs are NOT evil in and of themselves. But there is a problem...

With spaces I can control how "MY OWN CODE" looks in EVERY editor in the world. If I use 4 spaces -- then no matter what editor you open my code in, it will have the same distance from the left margin. With tabs I am at the mercy of the tab-width setting for the editor -- even for MY OWN CODE. And I don't like that.

So while it is true that even spaces can't guarantee consistency -- they at least afford you more control over the look of your OWN code everywhere -- something that tabs can't.

I think it's NOT the consistency in the programmers writing the code -- but the consistency in editors showing that code -- that spaces make easier to achieve (and impose).

偏爱自由 2024-07-12 03:10:51

我的猜测是,大多数 Linux 文本编辑器的默认值看起来大得离谱。 我想不出在制表符上使用空格的任何其他充分理由。

My guess is that most the linux text editors make defaults look ridiculously large by default. I can't think of any other good reason to use spaces over tabs.

给不了的爱 2024-07-12 03:10:51

好吧,我想说 PEP 8 中没有这样的“建议”。它被表述为建议,因为他们不会禁止你编写制表符,但由于代码必须以最标准化的方式编写,因此我们必须使用空格。

也就是说,如果我是编写标准指南的人,我会推荐制表符,因为它们是一种现代且更实用的代码缩进方式。

最后,我要强调的是,我不鼓励任何人使用制表符,相反,我是说我们所有人应该使用空格,如风格指南

Well, I would say that there is not such "recommendation" in the PEP 8. It is stated as a recommendation since they won't prohibit you to write tabs but since code must be written in the most standardized way, use spaces we must.

That said, if I were the one to write the standard guide, I would recommend tabs since they are a modern and more practical way to indent code.

Finally, I'll stress, I am not encouraging anybody to use tabs, instead, I am saying that all of us should use spaces as stated in the style guide.

热风软妹 2024-07-12 03:10:51

选项卡的普遍问题是它们在不同环境中的表示方式不同。
在给定的编辑器中,一个制表符可能是 8 个空格,也可能是 2 个空格。
在某些编辑器中,您可以控制它,而在其他编辑器中则不能。

选项卡的另一个问题是它们在打印输出中的表示方式。 我相信大多数打印机将制表符解释为 8 个空格。

有了空格,毫无疑问。 一切都会按照作者的意图排列。

The universal problem with tabs is that they can be represented differently in different environment.
In a given editor, a tab might be 8 spaces or it might be 2.
In some editors, you can control this, while in others you can't.

Another issue with tabs is how they are represented in printed output. I believe most printers interpret a tab as 8 spaces.

With spaces, there is no doubt. Everything will line up as the author intended.

挥剑断情 2024-07-12 03:10:51

我能说的空格相对于制表符的最重要的优点是,许多程序员和项目在源代码中使用一定数量的列,并且如果有人提交更改并将其制表符设置为 2 个空格,而项目使用 4 个空格作为tabstop 长行对于其他人的编辑器窗口来说太长了。 我同意选项卡更容易使用,但我认为空格更容易协作,这对于像 Python 这样的大型开源项目很重要。

The most significant advantage I can tell of spaces over tabs is that a lot of programmers and projects use a set number of columns for the source code, and if someone commits a change with their tabstop set to 2 spaces and the project uses 4 spaces as the tabstop the long lines are going to be too long for other people's editor window. I agree that tabs are easier to work with but I think spaces are easier for collaboration, which is important on a large open source project like Python.

夏日落 2024-07-12 03:10:51

JWZ 说得最好

当[人们]阅读代码时,当他们写完新代码时,他们关心当新作用域(或 sexpr 或其他)打开时代码倾向于缩进多少屏幕列...

...我的观点是,解决技术问题的最佳方法是强制要求 ASCII #9 TAB 字符永远不会出现在磁盘文件中:对编辑器进行编程,在写入行之前将 TAB 扩展为适当数量的空格到磁盘...

...这假设您从不在真正重要的地方使用制表符,例如在字符串或字符常量中,但我从不这样做:当重要的是它是制表符时,我总是使用 '\t'相反。

JWZ says it best:

When [people are] reading code, and when they're done writing new code, they care about how many screen columns by which the code tends to indent when a new scope (or sexpr, or whatever) opens...

...My opinion is that the best way to solve the technical issues is to mandate that the ASCII #9 TAB character never appear in disk files: program your editor to expand TABs to an appropriate number of spaces before writing the lines to disk...

...This assumes that you never use tabs in places where they are actually significant, like in string or character constants, but I never do that: when it matters that it is a tab, I always use '\t' instead.

逐鹿 2024-07-12 03:10:51

由于Python依赖缩进来识别程序结构,因此需要一种清晰的方法来识别缩进。 这就是选择空格或制表符的原因。

然而,Python 也有一种强烈的理念,那就是只有一种方法来做事情,因此应该有一个官方推荐的一种缩进方法。

空格和制表符都给编辑器处理缩进带来了独特的挑战。 选项卡本身的处理在编辑器甚至用户设置中并不统一。 由于空间不可配置,因此它们提出了更合乎逻辑的选择,因为它们保证结果在各处看起来都相同。

Since python relies on indentation in order to recognize program structure, a clear way to identify identation is required. This is the reason to pick either spaces or tabs.

However, python also has a strong philosophy of only having one way to do things, therefore there should be an official recommendation for one way to do indentation.

Both spaces and tabs pose unique challenges for an editor to handle as indentation. The handling of tabs themselves is not uniform across editors or even user settings. Since spaces are not configurable, they pose the more logical choice as they guarantee that the outcome will look everywhere the same.

诠释孤独 2024-07-12 03:10:51

请注意,选项卡的使用混淆了 PEP 8 的另一个方面:

将所有行限制为最多 79 个字符。

假设您使用制表符宽度为 2,而我使用制表符宽度为 8。您编写所有代码,使最长的行达到 79 个字符,然后我开始处理您的文件。 现在我的代码很难阅读,因为(正如 PEP 所说):

大多数工具中的默认换行会破坏代码的视觉结构

如果我们都使用 4 个空格,那么它总是相同的。 任何拥有支持 80 个字符宽度的编辑器的人都可以轻松地阅读代码。 注意:80 个字符的限制本身就是一场圣战,所以我们不要从这里开始。

任何不差劲的编辑器都应该有一个选项来使用空格,就好像它们是制表符一样(插入和制表符都可以)删除),所以这确实不应该是一个有效的论点。

Note that the use of tabs confuses another aspect of PEP 8:

Limit all lines to a maximum of 79 characters.

Let's say, hypothetically, that you use a tab width of 2 and I use a tab width of 8. You write all your code so your longest lines reach 79 characters, then I start to work on your file. Now I've got hard-to-read code because (as the PEP states):

The default wrapping in most tools disrupts the visual structure of the code

If we all use 4 spaces, it's ALWAYS the same. Anyone whose editor can support an 80 character width can comfortably read the code. Note: The 80 character limit is a holy war in and of itself, so let's not start that here.

Any non-sucky editor should have an option to use spaces as if they were tabs (both inserting and deleting), so that really shouldn't be a valid argument.

笛声青案梦长安 2024-07-12 03:10:51

问题的答案是:PEP-8 想要提出建议,并决定由于空格更受欢迎,因此将强烈推荐空格而不是制表符。


PEP-8 的注释

PEP-8 表示“每个缩进级别使用 4 个空格”。
很明显,这是标准建议。

“对于您不想搞乱的非常旧的代码,您可以继续使用 8 个空格制表符。”
很明显,在某些情况下可以使用选项卡。

“切勿混合使用制表符和空格。”
这是明确禁止混合的——我想我们都同意这一点。 Python 可以检测到这一点,并且经常会卡住。 使用 -tt 参数会导致此错误。

'Python 最流行的缩进方式是仅使用空格。 第二流行的方式是仅使用选项卡。'
这清楚地表明两者都被使用。 需要明确的是:您仍然不应该在同一文件中混合使用空格和制表符。

“对于新项目,强烈建议仅使用空格而不是制表符。”
这是一项明确的建议,也是一项强有力的建议,但并不是禁止使用制表符。


我在 PEP-8 中找不到我自己的问题的良好答案。
我使用制表符,我曾经在其他语言中使用过制表符。
Python 接受仅使用制表符的源代码。 这对我来说已经足够好了。

我想我应该尝试一下空间工作。 在我的编辑器中,我将文件类型配置为仅使用空格,因此如果我按 Tab 键,它会插入 4 个空格。 如果我按 Tab 太多次,我必须删除空格! 啊啊!删除次数是选项卡的四倍! 我的编辑无法判断我正在使用 4 个空格进行缩进(尽管一位编辑可能能够做到这一点),并且显然坚持一次删除一个空格。

难道Python不能被告知在读取缩进时将制表符视为n个空格吗?
如果我们能够就每个缩进 4 个空格和每个制表符 4 个空格达成一致,并允许 Python 接受这一点,那么就不会有问题。
我们应该找到双赢的解决问题的办法。

The answer to the question is: PEP-8 wants to make a recommendation and has decided that since spaces are more popular it will strongly recommend spaces over tabs.


Notes on PEP-8

PEP-8 says 'Use 4 spaces per indentation level.'
Its clear that this is the standard recommendation.

'For really old code that you don't want to mess up, you can continue to use 8-space tabs.'
Its clear that there are SOME circumstances when tabs can be used.

'Never mix tabs and spaces.'
This is a clear prohibition of mixing - I think we all agree on this. Python can detect this and often chokes. Using the -tt argument makes this an explicit error.

'The most popular way of indenting Python is with spaces only. The second-most popular way is with tabs only.'
This clearly states that both are used. Just to be ultra-clear: You should still never mix spaces and tabs in same file.

'For new projects, spaces-only are strongly recommended over tabs.'
This is a clear recommendation, and a strong one, but not a prohibition of tabs.


I can't find a good answer to my own question in PEP-8.
I use tabs, which I have used historically in other languages.
Python accepts source with exclusive use of tabs. That's good enough for me.

I thought I would have a go at working with spaces. In my editor, I configured a file type to use spaces exclusively and so it inserts 4 spaces if I press tab. If I press tab too many times, I have to delete the spaces! Arrgh! Four times as many deletes as tabs! My editor can't tell that I'm using 4 spaces for indents (although AN editor might be able to do this) and obviously insists on deleting the spaces one at a time.

Couldn't Python be told to consider tabs to be n spaces when its reading indentations?
If we could agree on 4 spaces per indentation and 4 spaces per tab and allow Python to accept this, then there would be no problems.
We should find win-win solutions to problems.

浮生面具三千个 2024-07-12 03:10:51

我一直在代码中使用制表符。 也就是说,我最近找到了使用空格的理由:在我的诺基亚 N900 互联网平板电脑上进行开发时,我现在拥有一个没有 Tab 键的键盘。 这迫使我要么复制并粘贴选项卡,要么用空格重新编写代码。
我在其他手机上也遇到过同样的问题。 诚然,这不是 Python 的标准用法,但需要记住。

I've always used tabs in my code. That said, I've recently found a reason to use spaces: When developing on my Nokia N900 internet tablet, I now had a keyboard without a tab key. This forced me to either copy and paste tabs or re-write my code with spaces.
I've run into the same problem with other phones. Granted, this is not a standard use of Python, but something to keep in mind.

温柔戏命师 2024-07-12 03:10:50

制表符的问题在于它们是不可见的,而且人们永远无法就制表符的宽度达成一致。 当您混合制表符和空格,并且在 Python 以外的其他位置设置制表位(每 8 个空格使用制表位)时,您将看到代码的布局与 Python 看到的布局不同。 由于布局决定了块,因此您将看到不同的逻辑。 它会导致微妙的错误。

如果你坚持违反 PEP 8 并使用制表符——或者更糟糕的是,混合制表符和空格——至少总是使用“-tt”参数运行 python,这会导致缩进不一致(有时是制表符,有时相同缩进级别的空格)会出现错误。 另外,如果可能的话,将编辑器设置为以不同方式显示选项卡。 但实际上,最好的方法是不要使用制表符,句号。

The problem with tabs is that they are invisible, and people can never agree on the width of tabs. When you mix tabs and spaces, and you set tabstops at something other than Python (which uses tabstops every 8 spaces) you will be seeing the code in a different layout than Python sees it. And because the layout determines blocks, you will be seeing different logic. It leads to subtle bugs.

If you insist on defying PEP 8 and using tabs -- or worse, mixing tabs and spaces -- at least always run python with the '-tt' argument, which makes inconsistent indentation (sometimes a tab, sometimes a space for the same indentation level) an error. Also, if possible, set your editor to display tabs differently. But really, the best approach is not to use tabs, period.

长安忆 2024-07-12 03:10:50

空格的原因是制表符是可选的。 空格是标点符号中实际的最小公分母。

每个像样的文本编辑器都有“用空格替换制表符”,很多人都使用它。 但不总是。

虽然某些文本编辑器可能会用制表符替换一串空格,但这种情况确实很少见。

底线。 空格是不会出错的。 您可能会在使用选项卡时出错。 因此不要使用制表符并减少出错的风险。

The reason for spaces is that tabs are optional. Spaces are the actual lowest-common denominator in punctuation.

Every decent text editor has a "replace tabs with spaces" and many people use this. But not always.

While some text editors might replace a run of spaces with a tab, this is really rare.

Bottom Line. You can't go wrong with spaces. You might go wrong with tabs. So don't use tabs and reduce the risk of mistakes.

奢望 2024-07-12 03:10:50

答案就在 PEP 中给出 [编辑:这段文字已在 中编辑掉2013]。 我引用:

最流行Python 缩进方式是仅使用空格。

您还需要什么其他根本原因?

不那么直白地说:还要考虑第一段中所述的 PEP 范围:

本文档提供了构成主要 Python 发行版中标准库的 Python 代码的编码约定。

目的是使官方 Python 发行版中的所有代码的格式保持一致(我希望我们能够同意这普遍是一件好事™)。

由于对于单个程序员来说,在空格和制表符之间做出决定 a) 实际上是一个品味问题 b) 可以通过技术手段(编辑器、转换脚本等)轻松处理,因此有一个明确的方法可以结束所有讨论:选择一个。

吉多是最佳选择。 他甚至不需要给出理由,但他还是参考了经验数据。

出于所有其他目的,您可以将此 PEP 作为建议,也可以忽略它——您的选择、您的团队的选择或您的团队领导的选择。

但如果我可以给你一个建议:不要混合它们;-) [编辑:混合制表符和空格不再是一种选择。]

The answer was given right there in the PEP [ed: this passage has been edited out in 2013]. I quote:

The most popular way of indenting Python is with spaces only.

What other underlying reason do you need?

To put it less bluntly: Consider also the scope of the PEP as stated in the very first paragraph:

This document gives coding conventions for the Python code comprising the standard library in the main Python distribution.

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™).

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 was the one to choose. He didn't even have to give a reason, but he still did by referring to empirical data.

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.

But if I may give you one advice: don't mix'em ;-) [ed: Mixing tabs and spaces is no longer an option.]

无人问我粥可暖 2024-07-12 03:10:50

我个人不同意制表符上有空格。 对我来说,制表符是一种文档布局字符/机制,而空格则用于代码中的内容或命令之间的划分。

我必须同意吉姆的评论,即制表符并不是真正的问题,而是人和问题。他们想如何混合制表符和空格。

也就是说,为了惯例,我强迫自己使用空格。 我重视一致性胜过个人偏好。

编辑 2022: 使用空格。 遵循语言约定以及您正在处理的特定项目中设置的约定。 使用 linter 来确保维护这些约定。 保存时格式化。 提交时的 Lint。 这将减少团队中的“自行车脱落”。 正如我多年前提到的,一致性胜过个人喜好

自行车脱落,也称为帕金森琐碎定律,描述了我们倾向于将过多的时间投入到卑微和琐碎的事情上,而忽视重要的事情。

I personally don't agree with spaces over tabs. To me, tabs are a document layout character/mechanism while spaces are for content or delineation between commands in the case of code.

I have to agree with Jim's comments that tabs aren't really the issue, it is people and how they want to mix tabs and spaces.

That said, I've forced myself to use spaces for the sake of convention. I value consistency over personal preference.

Edit 2022: Use spaces. Follow the language conventions and those set in the particular project you're working on. Use a linter to ensure those conventions are maintained. Format on save. Lint on commit. This will reduce "bikeshedding" on a team. As I mentioned so many years ago, consistency over personal preference!

Bikeshedding, also known as Parkinson’s law of triviality, describes our tendency to devote a disproportionate amount of our time to menial and trivial matters while leaving important matters unattended.

单身情人 2024-07-12 03:10:50

当混合使用制表符和空格时,会出现缩进的主要问题。 显然,这并没有告诉您应该选择哪个,但这是推荐一个的充分理由,即使您通过抛硬币来选择它。

然而,恕我直言,有一些次要原因更倾向于使用空格而不是制表符:

  • 不同的工具。 有时代码会显示在程序员编辑器之外。 例如。 发布到新闻组或论坛。 在这里,空格通常比制表符表现得更好 - 任何地方空格都会被破坏,制表符也一样,但反之则不然。

  • 程序员对源代码的看法不同。 这是非常主观的——它要么是选项卡的主要好处,要么是避免使用选项卡的原因,具体取决于你站在哪一边。 从好的方面来说,开发人员可以使用他们喜欢的缩进查看源代码,因此喜欢 2 空格缩进的开发人员可以与 8 空格的开发人员在同一源代码上合作,并且仍然可以按照自己的喜好查看源代码。 缺点是这会产生影响 - 有些人喜欢 8 空格,因为它给出了非常明显的反馈,表明它们嵌套得太深 - 他们可能会看到 2 缩进器签入的代码不断地在编辑器中换行。 让每个开发人员以相同的方式查看代码可以提高行长度的一致性,并且其他问题也很重要。

  • 继续行缩进。 有时您想要缩进一行以表明它是从前一行开始的。 例如。

    def foo(): 
          x = some_function_with_lots_of_args(foo, bar, baz, 
                                              xyzzy,等等) 
      

    如果使用制表符,则无法在不混合空格和制表符的情况下为在编辑器中使用不同制表符的人对齐。 这实际上消除了上述好处。

但显然,这是一个深深的宗教问题,也是编程所困扰的。 最重要的问题是我们应该选择一个——即使那不是你喜欢的。 有时我认为显着压痕的最大优点是至少我们可以避免支架放置的争论。

另外值得一读的是 Jamie Zawinski 关于这个问题的这篇文章。

The main problems with indentation occur when you mix tabs and spaces. Obviously this doesn't tell you which you should choose, but it is a good reason to to recommend one, even if you pick it by flipping a coin.

However, IMHO there are a few minor reasons to favour spaces over tabs:

  • Different tools. Sometimes code gets displayed outside of a programmer's editor. Eg. posted to a newsgroup or forum. Spaces generally do better than tabs here - everywhere spaces would get mangled, tabs do as well, but not vice-versa.

  • Programmers see the source differently. This is deeply subjective - its either the main benefit of tabs, or a reason to avoid them depending on which side you're on. On the plus side, developers can view the source with their preferred indentation, so a developer preferring 2-space indent can work with an 8-space developer on the same source and still see it as they like. The downside is that there are repercussions to this - some people like 8-space because it gives very visible feedback that they're too deeply nested - they may see code checked in by the 2-indenter constantly wrapping in their editor. Having every developer see the code the same way leads to more consistency wrt line lengths, and other matters too.

  • Continued line indentation. Sometimes you want to indent a line to indicate it is carried from the previous one. eg.

    def foo():
        x = some_function_with_lots_of_args(foo, bar, baz,
                                            xyzzy, blah)
    

    If using tabs, theres no way to align this for people using different tabstops in their editor without mixing spaces and tabs. This effectively kills the above benefit.

Obviously though, this is a deeply religious issue, which programming is plagued with. The most important issue is that we should choose one - even if thats not the one you favour. Sometimes I think that the biggest advantage of significant indentation is that at least we're spared brace placement flamewars.

Also worth reading is this article by Jamie Zawinski on the issue.

风蛊 2024-07-12 03:10:49

好吧,似乎每个人都对空间有强烈的偏见。
我专门使用选项卡。 我很清楚为什么。

制表符实际上是一项很酷的发明,它出现在空格之后。 它允许您缩进,而无需数百万次推入空格或使用假制表符(产生空格)。

我真的不明白为什么每个人都歧视标签的使用。 这很像老年人歧视年轻人选择更新、更高效的技术,并抱怨脉冲拨号适用于每部手机,而不仅仅是这些花哨的新手机。 “音频拨号并非在所有电话上都适用,这就是它错误的原因”。

您的编辑器无法正确处理选项卡? 好吧,找一个现代编辑器。 也许是时候了,我们现在已经进入了 21 世纪,编辑器只是高科技复杂软件的时代早已过去了。 我们现在有大量的编辑器可供选择,所有这些编辑器都支持选项卡。 此外,您还可以定义制表符的大小,这是空格无法做到的。 看不到标签? 这是什么争论? 好吧,你也看不到空格!

我可以大胆地建议找一个更好的编辑吗? 其中一款高科技产品已经在大约 10 年前发布,可以显示隐形字符? (讽刺结束)

使用空格会导致更多的删除和格式化工作。 这就是为什么(以及所有其他了解这一点并同意我观点的人)使用 Python 选项卡的原因。

混合制表符和空格是禁忌,也没有争议。 那是一团糟,永远行不通。

Well well, seems like everybody is strongly biased towards spaces.
I use tabs exclusively. I know very well why.

Tabs are actually a cool invention, that came after spaces. It allows you to indent without pushing space millions of times or using a fake tab (that produces spaces).

I really don't get why everybody is discriminating against the use of tabs. It is very much like old people discriminating against younger people for choosing a newer more efficient technology and complaining that pulse dialing works on every phone, not just on these fancy new ones. "Tone dialing doesn't work on every phone, that's why it is wrong".

Your editor cannot handle tabs properly? Well, get a modern editor. Might be darn time, we are now in the 21st century and the time when an editor was a high tech complicated piece of software is long past. We have now tons and tons of editors to choose from, all of them that support tabs just fine. Also, you can define how much a tab should be, a thing that you cannot do with spaces. Cannot see tabs? What is that for an argument? Well, you cannot see spaces neither!

May I be so bold to suggest to get a better editor? One of these high tech ones, that were released some 10 years ago already, that display invisible characters? (sarcasm off)

Using spaces causes a lot more deleting and formatting work. That is why (and all other people that know this and agree with me) use tabs for Python.

Mixing tabs and spaces is a no-no and no argument about that. That is a mess and can never work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文