Leading whitespace (spaces and tabs) at the beginning of a logical line is used to compute the indentation level of the line, which in turn is used to determine the grouping of statements.
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). 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.
首先, 制表符被转换成(从左到右)一至八个空格, 这样直到包括替换部分的字符总数是八的倍数(这个规则也适用于UNIX)第一个非空白字符前的空格总数用于检测代码的缩进, 缩进不能用反斜线在多个物理行间接续; 反斜线之前的空白字符用于检测缩进.
Cross-platform compatibility note: because of the nature of text editors on non-UNIX platforms, it is unwise to use a mixture of spaces and tabs for the indentation in a single source file. It should also be noted that different platforms may explicitly limit the maximum indentation level.
A formfeed character may be present at the start of the line; it will be ignored for the indentation calculations above. Formfeed characters occurring elsewhere in the leading whitespace have an undefined effect (for instance, they may reset the space count to zero).
The indentation levels of consecutive lines are used to generate INDENT and DEDENT tokens, using a stack, as follows.
连续行的缩进层次用于生成语言符号INDENT和DEDENT, 这个过程中使用了堆栈, 如下述:
Before the first line of the file is read, a single zero is pushed on the stack; this will never be popped off again. The numbers pushed on the stack will always be strictly increasing from bottom to top. At the beginning of each logical line, the line's indentation level is compared to the top of the stack. If it is equal, nothing happens. If it is larger, it is pushed on the stack, and one INDENT token is generated. If it is smaller, it must be one of the numbers occurring on the stack; all numbers on the stack that are larger are popped off, and for each number popped off a DEDENT token is generated. At the end of the file, a DEDENT token is generated for each number remaining on the stack that is larger than zero.
在文件第一行未读入之前, 一个零被压栈. 它以后也不会被弹出来. 堆栈中的数字都从底部向顶部增长, 在每个逻辑行的开头处, 行的缩进层次与栈顶比较, 如果相等则什么也不会发生; 如果大于栈顶, 将其压入栈中, 并产生一个INDENT语言符号;如果小于栈顶, 那么它应该是堆栈中已存在的数字中的一个, 堆栈中所有大于它的数都将被弹出, 并且每个都产生一个DEDENT语言符号, 到达文件尾时, 堆栈中大于零的数字都被弹出, 每弹出一个数字也产生一个DEDENT语言符号.
Here is an example of a correctly (though confusingly) indented piece of Python code:
def perm(l): # Compute the list of all permutations of l if len(l) <= 1: return [l] r =  for i in range(len(l)): s = l[:i] + l[i+1:] p = perm(s) for x in p: r.append(l[i:i+1] + x) return r
The following example shows various indentation errors:
def perm(l): # error: first line indented for i in range(len(l)): # error: not indented s = l[:i] + l[i+1:] p = perm(l[:i] + l[i+1:]) # error: unexpected indent for x in p: r.append(l[i:i+1] + x) return r # error: inconsistent dedent
(Actually, the first three errors are detected by the parser; only the
last error is found by the lexical analyzer -- the indentation of
return r does not match a level popped off the stack.)
(事实上, 前三个错误是由解析器发现的, 仅仅最后一个错误是由词法分析器找到的 -- return r 的缩进层次不与弹出堆栈中的数匹配.)