简体   繁体   English

Python对制表符和空格缩进的解释

[英]Python's interpretation of tabs and spaces to indent

I decided, that I learn a bit of Python.我决定,我学了一点 Python。 The first introduction says that it uses indentation to group statements.第一个介绍说它使用缩进来分组语句。 While the best habit is clearly to use just one of these what happens if I interchange them?虽然最好的习惯显然是只使用其中之一,如果我互换它们会发生什么? How many spaces will be considered equal to one tab?多少个空格将被视为等于一个制表符? Or will it fail to work at all if tabs and spaces are mixed?或者如果混合使用制表符和空格,它会完全无法工作吗?

Spaces are not treated as equivalent to tab.空格不被视为等同于制表符。 A line indented with a tab is at a different indentation from a line indented with 1, 2, 4 or 8 spaces.用制表符缩进的行与用 1、2、4或 8 个空格缩进的行缩进不同。

Proof by counter-example ( erroneous, or, at best, limited - tab != 4 spaces ):反例证明(错误的,或者充其量是有限的 - tab != 4 个空格):

x = 1
if x == 1:
^Iprint "fff\n"
    print "yyy\n"

The ' ^I ' shows a TAB . ' ^I ' 显示一个TAB When run through Python 2.5, I get the error:通过 Python 2.5 运行时,出现错误:

  File "xx.py", line 4
    print "yyy\n"
                ^
IndentationError: unindent does not match any outer indentation level

Thus showing that in Python 2.5, tabs are not equal to spaces (and in particular not equal to 4 spaces).因此表明在 Python 2.5 中,制表符不等于空格(特别是不等于 4 个空格)。


Oops - embarrassing;哎呀——尴尬; my proof by counter-example shows that tabs are not equivalent to 4 spaces.我的反例证明表明制表符不等于 4 个空格。 As Alex Martelli points out in a comment , in Python 2, tabs are equivalent to 8 spaces, and adapting the example with a tab and 8 spaces shows that this is indeed the case.正如Alex Martelli评论中指出的那样,在 Python 2 中,制表符等效于 8 个空格,并且使用制表符和 8 个空格来调整示例表明情况确实如此。

x = 1
if x != 1:
^Iprint "x is not 1\n"
        print "y is unset\n"

In Python 2, this code works, printing nothing.在 Python 2 中,此代码有效,不打印任何内容。


In Python 3, the rules are slightly different (as noted by Antti Haapala ).在Python 3,规则是稍有不同(如注意到安蒂哈帕拉)。 Compare:比较:

Python 2 says: Python 2 说:

First, tabs are replaced (from left to right) by one to eight spaces such that the total number of characters up to and including the replacement is a multiple of eight (this is intended to be the same rule as used by Unix).首先,制表符被一到八个空格替换(从左到右),这样包括替换在内的字符总数是八的倍数(这与 Unix 使用的规则相同)。 The total number of spaces preceding the first non-blank character then determines the line's indentation.第一个非空白字符之前的空格总数决定了该行的缩进。 Indentation cannot be split over multiple physical lines using backslashes;不能使用反斜杠将缩进拆分为多个物理行; the whitespace up to the first backslash determines the indentation.直到第一个反斜杠的空格决定了缩进。

Python 3 says: Python 3 说:

Tabs are replaced (from left to right) by one to eight spaces such that the total number of characters up to and including the replacement is a multiple of eight (this is intended to be the same rule as used by Unix).制表符由 1 到 8 个空格替换(从左到右),这样直到并包括替换的字符总数是 8 的倍数(这与 Unix 使用的规则相同)。 The total number of spaces preceding the first non-blank character then determines the line's indentation.第一个非空白字符之前的空格总数决定了该行的缩进。 Indentation cannot be split over multiple physical lines using backslashes;不能使用反斜杠将缩进拆分为多个物理行; the whitespace up to the first backslash determines the indentation.直到第一个反斜杠的空格决定了缩进。

(Apart from the opening word "First," these are identical.) (除了开头词“第一”,这些都是相同的。)

Python 3 adds an extra paragraph: Python 3 添加了一个额外的段落:

Indentation is rejected as inconsistent if a source file mixes tabs and spaces in a way that makes the meaning dependent on the worth of a tab in spaces;如果源文件混合制表符和空格的方式使意义依赖于空格中制表符的价值,则缩进将被拒绝为不一致; a TabError is raised in that case.在这种情况下会引发 TabError 。

This means that the TAB vs 8-space example that worked in Python 2 would generate a TabError in Python 3. It is best — necessary in Python 3 — to ensure that the sequence of characters making up the indentation on each line in a block is identical.这意味着在 Python 2 中工作的TAB与 8 空格示例将在 Python 3 中生成 TabError。最好——在 Python 3 中——确保构成块中每一行缩进的字符序列是相同。 PEP8 says 'use 4 spaces per indentation level'. PEP8表示“每个缩进级别使用 4 个空格”。 (Google's coding standards say 'use 2 spaces'.) (Google 的编码标准说“使用 2 个空格”。)

Follow PEP 8 for Python style.遵循PEP 8的 Python 风格。 PEP 8 says: Indentation PEP 8 说:缩进

Use 4 spaces per indentation level.每个缩进级别使用 4 个空格。

For really old code that you don't want to mess up, you can continue to use 8-space tabs.对于您不想弄乱的非常旧的代码,您可以继续使用 8 个空格的制表符。

Tabs or Spaces?制表符还是空格?

Never mix tabs and spaces.切勿混用制表符和空格。

The most popular way of indenting Python is with spaces only.最流行的 Python 缩进方式是仅使用空格。 The second-most popular way is with tabs only.第二种最流行的方式是仅使用选项卡。 Code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.使用制表符和空格混合缩进的代码应转换为仅使用空格。 When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces.使用 -t 选项调用 Python 命令行解释器时,它会发出有关非法混合制表符和空格的代码的警告。 When using -tt these warnings become errors.使用 -tt 时,这些警告会变成错误。 These options are highly recommended!强烈推荐这些选项!

In Python 2, the interpretation of TAB is as if it is converted to spaces using 8-space tab stops (as provided by previous answers already);在 Python 2 中, TAB的解释就像是使用 8 个空格的制表位将其转换为空格(如先前的答案已提供); that is that each TAB furthers the indentation by 1 to 8 spaces so that the resulting indentation is divisible by 8.也就是说,每个TAB将缩进进一步缩进 1 到 8 个空格,以便得到的缩进可以被 8 整除。

However this does not apply to Python 3 anymore - in Python 3 mixing of spaces and tabs are always an error - tabs only match tabs and spaces only match other spaces in indentation;然而,这不再适用于 Python 3——在Python 3 中混合空格和制表符总是一个错误——制表符只匹配制表符,而空格只匹配缩进中的其他空格; that is a block indented with TAB SPACE SPACE might contain a block indented with TAB SPACE SPACE TAB , but not one indented with TAB TAB TAB , it would be considered an indentation error, even though the block would seemingly extend further:这是一个用TAB SPACE SPACE缩进的块可能包含一个用TAB SPACE SPACE TAB缩进的块,但不是一个用TAB TAB TAB缩进的块,它会被认为是一个缩进错误,即使该块看起来会进一步扩展:

Indentation is rejected as inconsistent if a source file mixes tabs and spaces in a way that makes the meaning dependent on the worth of a tab in spaces;如果源文件混合制表符和空格的方式使意义依赖于空格中制表符的价值,则缩进将被拒绝为不一致; a TabError is raised in that case.在这种情况下会引发 TabError 。

Ie the algorithm works as follows:即算法的工作原理如下:

  • if both number of tabs and number of spaces matches the previous line (no matter the order), then this line belongs to the same block with the previous line如果两个数目的标签间隔数的匹配的前一行(不管顺序),则这条线属于与前行相同的块

  • if the number of one of (tabs, spaces) is greater than on the previous line and number of the other is at least equal to those on the previous line, this is an indented block如果(制表符,空格)之一的数量大于前一行,并且另一个的数量至少等于前一行的数量,则这是一个缩进块

  • the tuple (tabs, spaces) matches an indent from a previous block - this dedents to that block元组(tabs, spaces)与前一个块的缩进相匹配 - 这与该块相关

  • otherwise an IndentationError or a TabError is raised.否则会引发IndentationErrorTabError

This is why mixing tabs and spaces, or even using tabs for indentation at all would be considered a very bad practice in Python.这就是为什么混合制表符和空格,甚至完全使用制表符进行缩进在 Python 中被认为是一种非常糟糕的做法。

Just don't interchange them :)只是不要互换它们:)
Set your IDE/editor to input 4 spaces upon pressing "tab" and you are good to go.将您的 IDE/编辑器设置为在按下“tab”后输入 4 个空格,您就可以开始了。

I would recommend that you go through PEP 8 which is the 'official' Python style guide for Python code.我建议您阅读PEP 8 ,它是 Python 代码的“官方”Python 风格指南。 It covers (among other things) the use of tabs/spaces.它涵盖(除其他外)制表符/空格的使用。

Four spaces are one tab (in my setup), but as far as I know, they are not interchanged.四个空格是一个选项卡(在我的设置中),但据我所知,它们不会互换。 You can use either spaces or tabs, not both.您可以使用空格或制表符,但不能同时使用两者。

I believe that the tab character should simply never appear in source code under any circumstances.我相信制表符在任何情况下都不应该出现在源代码中。 There's no advantage to it and it's an endless source of tiny errors.它没有任何优势,它是微小错误的无尽来源。 - use a character string with \\t if you need a tab, it has the advantage that it's self-documenting. - 如果您需要制表符,请使用带有 \\t 的字符串,它的优点是它是自记录的。

Here 's the classic article about tabs vs spaces - I use a variant of jwz's elisp in my own .emacs file.是关于制表符与空格的经典文章 - 我在自己的 .emacs 文件中使用了 jwz 的 elisp 变体。

(I confess to personally breaking with PEP 8 by using only 2 characters' indentation - 4 characters is a lot when your lines are only 80 characters...) (我承认个人只使用 2 个字符的缩进打破了 PEP 8 - 当您的行只有 80 个字符时,4 个字符就很多了......)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM