17xie > Python 语言参考手册 > 2.1.5 显式行连接 Explicit line joining
背景:                 
[本书目录] [图书首页] [本书讨论区]  
链接地址:http://www.17xie.com/read-37402.html    注册17xie 一起来写书 实现您的出书梦想!


2.1.5 显式行连接 Explicit line joining

Two or more physical lines may be joined into logical lines using backslash characters (\), as follows: when a physical line ends in a backslash that is not part of a string literal or comment, it is joined with the following forming a single logical line, deleting the backslash and the following end-of-line character. For example:

两个或更多物理行可以使用反斜线字符(\)合并成一个逻辑行, 具体地说: 当一个物理行结束在一个反斜线(此时, 这个反斜线不能是串字面值或注释的一部分)处时,它就同下面的物理行合并成一个逻辑行,同时将反斜线和跟着的行结束符删除。

if 1900 < year < 2100 and 1 <= month <= 12 \
   and 1 <= day <= 31 and 0 <= hour < 24 \
   and 0 <= minute < 60 and 0 <= second < 60:   # Looks like a valid date
        return 1

A line ending in a backslash cannot carry a comment. A backslash does not continue a comment. A backslash does not continue a token except for string literals (i.e., tokens other than string literals cannot be split across physical lines using a backslash). A backslash is illegal elsewhere on a line outside a string literal.

以反斜线结尾的行不能在其后加注释, 反斜线不能续注释行. 除了串字面值, 也不能续语言符号(也就是说,其它不是串字面值的语言符号不能通过反斜线横跨数个物理行). 在串字面值之外其它地方出现的反斜线都是非法的.



2.1.6 隐式行连接 Implicit line joining

Expressions in parentheses, square brackets or curly braces can be split over more than one physical line without using backslashes. For example:

在小括号, 中括号, 大括号中的表达式可以跨越多个物理行, 而不用反斜线, 例如:

month_names = ['Januari', 'Februari', 'Maart',      # These are the
               'April',   'Mei',      'Juni',       # Dutch names
               'Juli',    'Augustus', 'September',  # for the months
               'Oktober', 'November', 'December']   # of the year

Implicitly continued lines can carry comments. The indentation of the continuation lines is not important. Blank continuation lines are allowed. There is no NEWLINE token between implicit continuation lines. Implicitly continued lines can also occur within triple-quoted strings (see below); in that case they cannot carry comments.

隐式的行连接可以尾随注释.接续行的缩进可以不考虑.空的接续行是不允许的.在隐式的接续行中是不存在NEWLINE语言符号的, 隐式的行连接在三重引用串(后述)中也是合法的, 那种情况下不能加注释.



2.1.7 空行 Blank lines

A logical line that contains only spaces, tabs, formfeeds and possibly a comment, is ignored (i.e., no NEWLINE token is generated). During interactive input of statements, handling of a blank line may differ depending on the implementation of the read-eval-print loop. In the standard implementation, an entirely blank logical line (i.e. one containing not even whitespace or a comment) terminates a multi-line statement.

一个仅包括空白, 制表符, 换页符和可能的注释的逻辑行, 是被忽略的(就是说没有NEWLINE语言符号产生). 在交互式输入语句时, 空行的处理可能不同, 依赖于输入-计算-输出(read-eval-print)循环的实现方式. 在标准实现中, 一个纯粹的空行(即不包括任何东西,甚至注释和空白)才结束多行语句.



2.1.8 缩进 Indentation

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.

跨平台兼容性注意:由于在非UNIX平台上的文本编辑器特性,在单个源文件里使用混合空格和制表符的缩进是不明智的。

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:

这是一个有着正确缩进格式的Python代码的例子(虽然有点乱):

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 的缩进层次不与弹出堆栈中的数匹配.)



2.1.9 符号间的空白 Whitespace between tokens

Except at the beginning of a logical line or in string literals, the whitespace characters space, tab and formfeed can be used interchangeably to separate tokens. Whitespace is needed between two tokens only if their concatenation could otherwise be interpreted as a different token (e.g., ab is one token, but a b is two tokens).

除非位于逻辑行起首或者字符串当中,空格,制表符和换页符这些空白字符可以等同地用于分隔词法项(token)。 空白仅当两个符号在连接时会有其它含义时才使用(例如, ab是一个符号, 但a b是两个符号).


字数:6916    最后更新:8个月以前 [03-15 20:12]月落晨星 修改
本页编辑者:月落晨星  
[前一页]:2.1 行结构 Line stru…  [后一页]:2.2 其它语言符号 Oth…
[在本页中加入书签] [收藏本书] [推荐本书]
  17xie论坛 > 本书讨论区 > 本页评论   (共0条)
发表评论

用户名称 匿名发表
评论内容
验证码

关于我们 | 版权声明 | 免责声明 | 诚聘英才 | 联系我们 | 合作伙伴 | 友情链接 | 广告合作 | 提交意见
Copyright © 2007 17xie.com 互联网协同写书平台 京ICP备08002671号