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


5.2 原子 Atoms

Atoms are the most basic elements of expressions. The simplest atoms are identifiers or literals. Forms enclosed in reverse quotes or in parentheses, brackets or braces are also categorized syntactically as atoms. The syntax for atoms is:

原子是表达式最基本的组成单位. 最简单的原子是标识符或者字面值. 以反引号,圆括号, 方括号或大括号括住的符号在句法上也看成是原子.原子的句法如下:

Download entire grammar as text.


5.2.1 标识符(名字) Identifiers (Names)

An identifier occurring as an atom is a name. See section 4.1 for documentation of naming and binding.

作为一个原子出现的标识符是对一个局部名字, 或全局名字或内建名字捆绑的引用. 如果该名字出现在某代码块的任意的一个地方(即使是在不可达的代码中), 而且它未在global语句中的话, 那么它就是该代码块的局部名字. 当它没在代码块被赋值,或者虽然被赋值但是它是在globals语句中显式地列出的话, 它就是引用的一个全局名字(如果它存在), 或者一个内建名字(这个捆绑规则可以动态改变).5.1

When the name is bound to an object, evaluation of the atom yields that object. When a name is not bound, an attempt to evaluate it raises a NameError exception.

当某名字捆绑的是一个对象时, 使用该原子就是使用那个对象. 当某名字没有捆绑就直接使用它, 则会抛出一个NameError异常.

私有名字变换: Private name mangling: When an identifier that textually occurs in a class definition begins with two or more underscore characters and does not end in two or more underscores, it is considered a private name of that class. Private names are transformed to a longer form before code is generated for them. The transformation inserts the class name in front of the name, with leading underscores removed, and a single underscore inserted in front of the class name. For example, the identifier __spam occurring in a class named Ham will be transformed to _Ham__spam. This transformation is independent of the syntactical context in which the identifier is used. If the transformed name is extremely long (longer than 255 characters), implementation defined truncation may happen. If the class name consists only of underscores, no transformation is done.

在类定义中, 以两个或多个下划线开始, 并且尾部不是以两个或多个下划线结束的标识符, 它被看作是类的私有名字. 在产生它的代码之前, 私有名字被变换成更长的形式. 这种变换是在将去掉前导下划线的类名插入到名字前,再在类名前插入一个下划线。 .例如, 在类Ham中定义的标识符__spam, 会被变换成_Ham__spam. 本变换是不依赖于使用该标识符处代码的句法上的上下文的. 如果变换后的结果过长(超过255个字符), 就会执行该Python实现定义的截短名字的操作. 如果某类的名字仅仅由下划线组成, 这种变换是不会发生的.



5.2.2 字面值 Literals

Python supports string literals and various numeric literals:

Python支持字符串字面值和数值型字面值:

Download entire grammar as text.

Evaluation of a literal yields an object of the given type (string, integer, long integer, floating point number, complex number) with the given value. The value may be approximated in the case of floating point and imaginary (complex) literals. See section 2.4 for details.

使用一个字面值会得到一个具有给定值的相应类型的对象(字符串、整数、长整数、浮点数、复数),如果是浮点数和复数那么这个值可能是个近似值,详见2.4节。

All literals correspond to immutable data types, and hence the object's identity is less important than its value. Multiple evaluations of literals with the same value (either the same occurrence in the program text or a different occurrence) may obtain the same object or a different object with the same value.

所有字面值都属于不可变的数据类型,因此对象的标识比起它们的值来说显得次要一些。多次使用相同值的字面值(在程序代码中以相同形式出现或者以不同的形式出现)可能获得的是相同的对象或具有相同值的不同对象。



5.2.3 括号表达式 Parenthesized forms

A parenthesized form is an optional expression list enclosed in parentheses:

一个括号表达式是位于一对小括号内可选的表达式表.

parenth_form  ::=  "(" [expression_list] ")"
Download entire grammar as text.

A parenthesized expression list yields whatever that expression list yields: if the list contains at least one comma, it yields a tuple; otherwise, it yields the single expression that makes up the expression list.

表达式表生成什么类型的值括号表达式也就生成什么类型的值:如果表达式表中包括了至少一个逗号, 它就生成一个元组; 否则, 就生成那个组成表达式表的唯一的表达式.

An empty pair of parentheses yields an empty tuple object. Since tuples are immutable, the rules for literals apply (i.e., two occurrences of the empty tuple may or may not yield the same object).

一个空的表达式表会生成一个空的元组对象.因为元组是不可变的, 因此这里适用字符串所用的规则(即, 两个具有空的元组可能是同一个对象也可能是不同的对象).

Note that tuples are not formed by the parentheses, but rather by use of the comma operator. The exception is the empty tuple, for which parentheses are required -- allowing unparenthesized ``nothing'' in expressions would cause ambiguities and allow common typos to pass uncaught.

请注意元组不是依靠小括号成定义的, 而使用逗号. 其中空元组是个例外, 此时要求有小括号 -- 在表达式中允许没有小括号的"空"可能会引起歧义,并容易造成难以查觉的笔误.


字数:4454    最后更新:8个月以前 [03-15 20:47]月落晨星 修改
本页编辑者:月落晨星  
[前一页]:5. 表达式 Expression…  [后一页]:5.2.4 列表的表示 Lis…
[在本页中加入书签] [收藏本书] [推荐本书]
  17xie论坛 > 本书讨论区 > 本页评论   (共0条)
发表评论

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

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