17xie > Python 语言参考手册 > 2.1 行结构 Line structure
背景:                 
[本书目录] [图书首页] [本书讨论区]  
链接地址:http://www.17xie.com/read-37401.html    注册17xie 一起来写书 实现您的出书梦想!


2.1 行结构 Line structure

A Python program is divided into a number of logical lines.

一个Python程序被分成多个逻辑行.




2.1.1 逻辑行 Logical lines

The end of a logical line is represented by the token NEWLINE. Statements cannot cross logical line boundaries except where NEWLINE is allowed by the syntax (e.g., between statements in compound statements). A logical line is constructed from one or more physical lines by following the explicit or implicit line joining rules.

逻辑行以一个NEWLINE(新行)语言符号结束, 语句不能跨多个逻辑行, 除非语法上允许NEWLINE(例如, 在复合语句的中的语句序列).一个逻辑行由一个物理行, 或者以显式/隐式行连接规则连接的多个物理行构成.



2.1.2 物理行 Physical lines

A physical line ends in whatever the current platform's convention is for terminating lines. On Unix, this is the ASCII LF (linefeed) character. On Windows, it is the ASCII sequence CR LF (return followed by linefeed). On Macintosh, it is the ASCII CR (return) character.

一个物理行由所在平台的断行符号结束.在Unix上, 是ASCII LF(换行)字符; 在DOS/Windows上, 是ASCII字符序列CR LF(回车加换行); 在Macintosh上, 是ASCII CR(回车)字符.



2.1.3 注释 Comments

A comment starts with a hash character (#) that is not part of a string literal, and ends at the end of the physical line. A comment signifies the end of the logical line unless the implicit line joining rules are invoked. Comments are ignored by the syntax; they are not tokens.

一个注释以#字符(此时, 它不能是串字面值的一部分)开始,结束于该物理行的结尾.如果没有隐式的行连接, 那么注释就意味着该逻辑行的终止.注释为句法分析所忽略, 它们不记作语言符号.



2.1.4 Encoding declarations

If a comment in the first or second line of the Python script matches the regular expression coding[=:]\s*([\w-_.]+), this comment is processed as an encoding declaration; the first group of this expression names the encoding of the source code file. The recommended forms of this expression are

# -*- coding: <encoding-name> -*-

which is recognized also by GNU Emacs, and

# vim:fileencoding=<encoding-name>

which is recognized by Bram Moolenar's VIM. In addition, if the first bytes of the file are the UTF-8 byte-order mark ('\xef\xbb\xbf'), the declared file encoding is UTF-8 (this is supported, among others, by Microsoft's notepad).

If an encoding is declared, the encoding name must be recognized by Python. The encoding is used for all lexical analysis, in particular to find the end of a string, and to interpret the contents of Unicode literals. String literals are converted to Unicode for syntactical analysis, then converted back to their original encoding before interpretation starts. The encoding declaration must appear on a line of its own.


字数:2494    最后更新:8个月以前 [03-15 20:10]月落晨星 修改
本页编辑者:月落晨星  
[前一页]:2. 词法分析 Lexical …  [后一页]:2.1.5 显式行连接 Exp…
[在本页中加入书签] [收藏本书] [推荐本书]
  17xie论坛 > 本书讨论区 > 本页评论   (共0条)
发表评论

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

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