17xie > Python 语言参考手册 > 6. 简单语句 Simple statements
背景:                 
[本书目录] [图书首页] [本书讨论区]  
链接地址:http://www.17xie.com/read-37447.html    注册17xie 一起来写书 实现您的出书梦想!


6. 简单语句 Simple statements

Simple statements are comprised within a single logical line. Several simple statements may occur on a single line separated by semicolons. The syntax for simple statements is:

简单语句可以在一个逻辑行中表达. 某些简单语句可以用分号分隔而占据一行.简单语句句法如下:

Download entire grammar as text.


6.1 表达式语句 Expression statements

Expression statements are used (mostly interactively) to compute and write a value, or (usually) to call a procedure (a function that returns no meaningful result; in Python, procedures return the value None). Other uses of expression statements are allowed and occasionally useful. The syntax for an expression statement is:

表达式语句用于计算和写一个值(多用在交互方式下), 或调用一个过程(一个返回没有意义的结果的函数; 在Python中, 过程返回值None). 其它表达式语句的使用方法也允许, 有时也用的到.表达式语句的句法如下:

Download entire grammar as text.

An expression statement evaluates the expression list (which may be a single expression).

一个表达式语句要对该表达表(可能只是一个表达式)求值.

In interactive mode, if the value is not None, it is converted to a string using the built-in repr()function and the resulting string is written to standard output (see section 6.6) on a line by itself. (Expression statements yielding None are not written, so that procedure calls do not cause any output.)

在交互方式下, 如果其值不为None, 就使用内建函数repr()并将结果串写入标准输出(见6.6节), 返回None的过程不回写, 所有过程调用没有任何输出.)



6.2 断言语句 Assert statements

Assert statements are a convenient way to insert debugging assertions into a program:

断言语句是一个在程序中插入调试断言的常用方法:

assert_stmt  ::=  "assert" expression ["," expression]
Download entire grammar as text.

The simple form, "assert expression", is equivalent to

简单形式的, "assert expression", 等价于:

if __debug__:
   if not expression: raise AssertionError

The extended form, "assert expression1, expression2", is equivalent to

扩展形式的, "assert expression", 等价于:

if __debug__:
   if not expression1: raise AssertionError, expression2

These equivalences assume that __debug__and AssertionError refer to the built-in variables with those names. In the current implementation, the built-in variable __debug__ is 1 under normal circumstances, 0 when optimization is requested (command line option -O). The current code generator emits no code for an assert statement when optimization is requested at compile time. Note that it is unnecessary to include the source code for the expression that failed in the error message; it will be displayed as part of the stack trace.

这些等价式假定了存在 __debug__ 和 AssertionError , 而不是具有相同名字的相应内建变量.在当前实现, 内建变量__debug__在普通情况下为1, 在要求优化的情况下为0(命令行选项-O) 在编译要求优化时, 当前的代码生成器不产生任何断言语句的代码.注意在错误信息包括源代码的作法是多余的; 因为它们会作为跟踪回溯对象的一部分显示.

Assignments to __debug__ are illegal. The value for the built-in variable is determined when the interpreter starts.

给__debug__赋值是非法的,解释器是在启动时读取内建变量的值的.


字数:3324    最后更新:8个月以前 [03-15 21:08]月落晨星 修改
本页编辑者:月落晨星  
[前一页]:5.12 表达式表 Expres…  [后一页]:6.3 赋值语句 Assignm…
[在本页中加入书签] [收藏本书] [推荐本书]
  17xie论坛 > 本书讨论区 > 本页评论   (共0条)
发表评论

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

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