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

Luk*_*kas 32 python tabs spaces indentation

我决定,我学习了一点Python.第一个介绍说它使用缩进来分组语句.虽然最好的习惯显然只使用其中一种,如果我互换它会发生什么?多少个空格将被视为等于一个标签?如果标签和空格混合在一起会不会起作用?

Jon*_*ler 35

空格不被视为与制表符等效.使用制表符缩进的行与缩进为1,2,4 或8个空格的行的缩进位置不同.

通过反例证明(错误的,或者,最好是有限的 - 标签!= 4个空格):

x = 1
if x == 1:
^Iprint "fff\n"
    print "yyy\n"
Run Code Online (Sandbox Code Playgroud)

' ^I'显示了一个TAB.通过Python 2.5运行时,我收到错误:

  File "xx.py", line 4
    print "yyy\n"
                ^
IndentationError: unindent does not match any outer indentation level
Run Code Online (Sandbox Code Playgroud)

因此显示在Python 2.5中,制表符不等于空格(特别是不等于4个空格).


哎呀 - 尴尬; 我通过反例的证明表明,标签不等于4个空格.正如Alex Martelli评论中指出的那样,在Python 2中,制表符相当于8个空格,并且使用制表符和8个空格调整示例表明情况确实如此.

x = 1
if x != 1:
^Iprint "x is not 1\n"
        print "y is unset\n"
Run Code Online (Sandbox Code Playgroud)

在Python 2中,此代码有效,无需打印.


在Python 3,规则是稍有不同(如注意到安蒂哈帕拉).相比:

Python 2说:

首先,标签被替换(从左到右)一到八个空格,使得直到并包括替换的字符总数是八的倍数(这与Unix使用的规则相同).然后,第一个非空白字符前面的空格总数确定行的缩进.缩进不能使用反斜杠在多个物理行上分割; 直到第一个反斜杠的空格确定缩进.

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

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.

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. PEP8 says 'use 4 spaces per indentation level'. (Google's coding standards say 'use 2 spaces'.)


Eli*_*sky 18

Follow PEP 8 for Python style. PEP 8 says: Indentation

每个缩进级别使用4个空格.

对于您不想搞砸的真正旧代码,您可以继续使用8空格选项卡.

标签或空格?

切勿混合标签和空格.

缩进Python最流行的方法是仅使用空格.第二种最流行的方式是仅使用标签.使用制表符和空格混合缩进的代码应转换为仅使用空格.当使用-t选项调用Python命令行解释器时,它会发出有关非法混合制表符和空格的代码的警告.使用-tt时,这些警告会出错.强烈推荐这些选项!


Ant*_*ala 8

在Python 2中,解释TAB就像使用8空格制表符将其转换为空格(由之前的答案提供); 也就是说,每一个都TAB将缩进推进1到8个空格,这样得到的缩进就可以被8整除.

但是这不再适用于Python 3 - 在Python 3中,空格和制表符的混合总是错误的 - 制表符只匹配制表符,空格只与缩进中的其他空格匹配; 这是一条缩进的行,TABSPACESPACE也可以包含缩进的行SPACESPACETAB; 并且可能包含缩进的缩进块TABSPACESPACETAB,但如果它包含缩进块TABTAB,则会将其视为缩进错误,即使该块看起来会进一步扩展:

如果源文件以一种使得含义依赖于空格中制表符的价值的方式混合制表符和空格,则缩进被拒绝为不一致; 在这种情况下会引发TabError.

即算法的工作原理如下:

  • 如果两个数目的标签空格数前一行(无论顺序)相匹配,那么这行属于与前行相同的块

  • 如果(制表符,空格)中的一个的数量大于前一行的数量,而另一个的数量至少等于前一行的数量,则这是一个缩进的块

  • 元组(tabs, spaces)匹配来自前一个块的缩进 - 这个块就是该块

  • 否则a IndentationError或a TabError被提出.

这就是混合制表符和空格,甚至使用制表符进行缩进的原因,在Python中被认为是一种非常糟糕的做法.


shy*_*ent 5

Just don't interchange them :)
Set your IDE/editor to input 4 spaces upon pressing "tab" and you are good to go.


Nou*_*him 5

I would recommend that you go through PEP 8 which is the 'official' Python style guide for Python code. It covers (among other things) the use of tabs/spaces.