5. 表达式

这一章解释Python中表达式的各个组成部分的含义。

关于语法:在这一章及随后的章节中所用到的扩展BNF语法符号将用于讲述语法,而不是词法分析。当一个语法规则具有这样的形式

name ::=  othername

且没有给出语义,那么这种形式的name语义与othername相同。

5.1. 算术转换

当下面算术操作符的描述使用短语“数字参数被转换为一个共同的类型”时,这些参数将使用隐式转换规则列出的规则做隐式转换。 如果两个参数都是标准的数字类型,那么运用下面的隐式转换:

  • 如果任意一个参数是复数,将另外一个转换成复数;
  • 否则,如果任意一个参数是浮点数,则将另外一个转换为浮点数;
  • 否则,如果任意一个是长整数,则将另外一个转换成长整数;
  • 否则,两个参数必定都是普通的整数且不必要转换。

某些特定的操作符适用其它的一些规则(例如,‘%’操作符左边的字符串参数)。解释器的扩展可以定义它们自己的转换规则。

5.2. 原子

原子是表达式最基础的元素。最简单的原子是标识符和字面值。在引号、圆括号、方括号或者花括号中的封闭形式在语法上也被分类为原子。原子的语法为:

atom      ::=  identifier | literal | enclosure
enclosure ::=  parenth_form | list_display
               | generator_expression | dict_display | set_display
               | string_conversion | yield_atom

5.2.1. 标识符(名称)

一个以原子出现的标识符是一个名称。词法定义请参看标识符和关键字小节,名称和绑定的文档请参看名称和绑定小节。

当名称绑定到一个对象上时,对该原子的求值将产生那个对象。当名称没有绑定时,试图对它求值将抛出NameError异常。

私有变量名称的改编:当出现在类定义中的标识符以两个或多个下划线字符开始且不是以两个或多个下划线结束,它被认为是那个类的私有名称在为它们生成代码之前,私有名称被转换为更长的形式。该转换在名称的前面插入类的名称,前导的下滑线被删除并插入一个单一的下划线。例如,出现在类Ham中的标识符__spam将被转换成_Ham__spam这种转换与标识符使用的语法上下文无关。如果转换后的名称过长(超过255个字符),将可能发生与具体实现有关的截断。如果类的名称只由下划线组成,则不会转换。

5.2.2. 字面量

Python支持字符串字面值和各种数值字面值:

literal ::=  stringliteral | integer | longinteger
             | floatnumber | imagnumber

在浮点数和虚数(复数)情况下,可能只是近似值。详细信息参见字面量一节。

所有的字面量都是不可变数据类型,因此对象的ID不如它的值重要。多次计算具有相同值的字面值(无论是程序文本中相同的出现还是不同的出现)得到的既可能是同一个对象也可能是具有相同值的不同对象。

5.2.3. 圆括号表达式

圆括号表达式是括号里面包含一个可选的表达式列表:

parenth_form ::=  "(" [expression_list] ")"

圆括号中的表达式序列产生的就是该表达式序列产生的内容:如果序列包含至少一个逗号,那么它产生一个元组;否则,它产生组成表达式列表的单一的表达式。

空的圆括号对产生空的元组对象;因为元组是不可变的,字面量的规则同样适用(即,空元组的两次出现可能产生相同或不同的对象)。

注意元组不是通过圆括号而是逗号操作符形成。有个例外是空元组,它必须要有圆括号 — 允许表达式中出现没有括号的“nothing”将导致歧义并使得常见的拼写错误无法发现。

5.2.4. List displays

列表表示式是在方括号中的可以为空的一系列表达式:

list_display        ::=  "[" [expression_list | list_comprehension] "]"
list_comprehension  ::=  expression list_for
list_for            ::=  "for" target_list "in" old_expression_list [list_iter]
old_expression_list ::=  old_expression [("," old_expression)+ [","]]
old_expression      ::=  or_test | old_lambda_expr
list_iter           ::=  list_for | list_if
list_if             ::=  "if" old_expression [list_iter]

列表的列举产生一个新的列表对象。它的内容通过提供一个表达式序列或者一个列表推导式指定。当提供的是一个逗号分隔的表达式序列时,对它的元素从左向右求值并按此顺序放入列表对象中。当提供的是一个列表推导式时,它由一个单一的表达式后面跟着至少一个for子句和零个或多个for或者if子句组成。在这种情况下,新的列表的元素是由for或者if子句块产生,这些子句块从左向右嵌套,且当到达最内层的代码块时对表达式求值以产生一个列表元素[1]

5.2.5. Displays for sets and dictionaries

对于构造集合和字典,Python提供特殊的语法叫做“表示式",它们有两种方式:

  • 容器的内容被显式地列出,或者
  • 它们通过一系列循环和过滤指令计算得到,这种方式叫做推导式

推导式常见的语法元素为:

comprehension ::=  expression comp_for
comp_for      ::=  "for" target_list "in" or_test [comp_iter]
comp_iter     ::=  comp_for | comp_if
comp_if       ::=  "if" expression_nocond [comp_iter]

推导式由一个单一的表达式后面跟随至少一个for子句加上零个或多个for或者if子句。在这种情况下,新的列表的元素是由for或者if子句块产生,这些子句块从左向右嵌套,且当到达最内层的代码块时对表达式求值以产生一个列表元素。

注意,推导式在一个单独的作用域中执行,所以在目标序列中赋值的名称不会“泄露”到外围的作用域中。

5.2.6. Generator expressions

生成器表达式是在圆括号中的一个简洁的生成器符号:

generator_expression ::=  "(" expression comp_for ")"

生成器表达式产生一个新的生成器对象。它的语法与推导式相同,只是它位于圆括号而不是方括号或花括号中。

生成器表达式中使用的变量在为生成器对象调用__next__()方法时才会惰性地求值(与普通的生成器方式相同)。但是,最左边的for子句会立即计算,所以它产生的错误可以在生成器表达式代码中的任何其它可能的错误之前发现。随后的for子句不可以立即计算因为它们可能依赖于前面的for循环。例如:(x*y for x in range(10) for y in bar(x))

圆括号对于只有一个参数的调用可以省略。细节请参考调用一节。

5.2.7. Dictionary displays

字典表示式是在花括号中的可以为空的一系列键/值对。

dict_display       ::=  "{" [key_datum_list | dict_comprehension] "}"
key_datum_list     ::=  key_datum ("," key_datum)* [","]
key_datum          ::=  expression ":" expression
dict_comprehension ::=  expression ":" expression comp_for

字典表示式产生一个新的字典对象。

如果给出逗号分隔的键/值对序列,将从左向右对它们求值以定义字典中项:用每个键对象作为字典的键并存储对应的值。这意味着你可以在键/值序列中多次指定相同的键,但是该键最终对应的字典的值将是最后给出的那个值。

字典推导式,与列表和集合推导式相比,需要两个被冒号分隔的表达式并在后面跟随通常的“for”和“if”子句。当推导执行时,产生的键和值以它们生成的顺序插入到新的字典中。

键的类型的限制在前面的标准类型的层次一节中有列出。(简要地讲,键的类型应该是可哈希的,即排除所有可变的对象。)重复的键之间的冲突不会被检测到;一个给定的键的最后的值(表示式中最右边的值)将获胜。

5.2.8. Set displays

集合表示式通过花括号表明,与字典表示式的区别是缺少冒号分隔的键和值:

set_display ::=  "{" (expression_list | comprehension) "}"

集合表示式产生一个新的可变集合对象,它的内容可以通过一个表达式序列或者一个推导式指定。当提供的是一个逗号分隔的表达式序列时,将从左向右计算它的元素并添加到集合对象中。当提供的是一个推导式时,集合根据推导式产生的元素构造。

不可以用{}构造一个空集合;该字面值构造一个空的字典。

5.2.9. 字符串转换式

字符串转换式是包含在反引号中的一个表达式序列:

string_conversion ::=  "`" expression_list "`"

字符串转换式计算包含的表达式序列并根据结果对象类型的特定规则将结果对象转换成一个字符串。

If the object is a string, a number, None, or a tuple, list or dictionary containing only objects whose type is one of these, the resulting string is a valid Python expression which can be passed to the built-in function eval() to yield an expression with the same value (or an approximation, if floating point numbers are involved).

(特别地,字符串转换式会添加引号并将“古怪”的字符转换为转义的序列,这些序列打印出来是安全的。)

递归的对象(例如,直接或间接包含自身引用的列表或字典)使用...来表示一个递归的引用,其结果不可以传递给eval()以获得一个相等的值(将引发SyntaxError)。

内建函数repr()对其参数所做的转换与将它放入圆括号和反引号中完全相同。内建函数str()完成类似但更友好的转换。

5.2.10. Yield expressions

yield_atom       ::=  "(" yield_expression ")"
yield_expression ::=  "yield" [expression_list]

2.5 版中新增。

yield表达式只用于定义生成器函数,且只能用于函数的定义体中。在函数定义中使用yield表达式就可以充分使得该定义创建一个生成器函数而不是普通的函数。

当调用生成器函数时,它返回一个称为生成器的迭代器。然后该生成器控制生成器函数的执行。当调用生成器的其中一个方法时,执行开始。此时,执行会行进到第一个yield表达式,在那里执行被挂起并返回expression_list的值给生成器的调用者。挂起的意思是保存所有的局部状态,包括当前局部变量的绑定、指令的指针和内部的计算栈。当通过调用生成器的一个方法来恢复执行时,函数可以准确地继续执行就好像yield表达式只是一个外部的调用。恢复执行后yield表达式的值取决于恢复执行的方法。

所有这些使得生成器函数与协程非常类似;它们可以yield多次,它们有多个入口点且它们的执行可以挂起。唯一的区别是生成器函数不可以控制yield之后执行应该从何处继续;控制始终被转让给生成器的调用者。

5.2.10.1. Generator-iterator methods

该小节讲述生成器迭代器的方法。它们可用于控制生成器函数的执行。

注意当生成器已经在执行时调用下面的任何一个生成器方法都将引发ValueError异常。

class generator
generator.next()

开始生成器函数的执行或者在最后一次执行的yield表达式处恢复执行。当生成器函数使用next()方法恢复执行时,当前的yield表达式始终None然后执行继续行进到下一个yield表达式,在那里生成器被再次挂起并返回expression_list的值给next()的调用者。如果生成器退出时没有yield另外一个值,则引发一个StopIteration异常。

.
generator.send(value)

恢复执行并“发送”一个值到生成器中。value参数成为当前yield表达式的结果。send()方法返回生成器yield的下一个值,如果生成器退出时没有yield另外一个值则引发StopIteration当调用send()用于(第一次)开始生成器的执行时,它必须以None作为参数进行调用,因为没有接受该值的yield表达式。

generator.throw(type[, value[, traceback]])

在生成器暂停的地方引发一个type类型的异常,并返回生成器函数yield的下一个值。如果生成器在退出时没有yield一个值,则引发StopIteration异常。如果生成器函数没有捕获传递进来的异常或者引发一个不同的异常,那么该异常将传播到调用者。

generator.close()

在生成器函数暂停的地方引发一个GeneratorExit如果生成器函数此后引发StopIteration(正常退出或者由于已经正在关闭)或者GeneratorExit(没有捕获该异常),close会返回到调用者。如果生成器yield一个值,则引发一个RuntimeError如果生成器引发其它任何异常,它会被传播到调用者。如果生成器已经由于异常退出或正常退出,close()不会做任何事情。

Here is a simple example that demonstrates the behavior of generators and generator functions:

>>> def echo(value=None):
...     print "Execution starts when 'next()' is called for the first time."
...     try:
...         while True:
...             try:
...                 value = (yield value)
...             except Exception, e:
...                 value = e
...     finally:
...         print "Don't forget to clean up when 'close()' is called."
...
>>> generator = echo(1)
>>> print generator.next()
Execution starts when 'next()' is called for the first time.
1
>>> print generator.next()
None
>>> print generator.send(2)
2
>>> generator.throw(TypeError, "spam")
TypeError('spam',)
>>> generator.close()
Don't forget to clean up when 'close()' is called.

See also

PEP 0342 - 通过增强的生成器实现协程
增强生成器API和语法的提议,使得它们可以作为简单的协程使用。

5.3. 初级操作

初级操作表示语言中绑定性最高的操作。Their syntax is:

primary ::=  atom | attributeref | subscription | slicing | call

5.3.1. Attribute references

属性引用是一个初级操作,后面跟随一个句号和一个名称:

attributeref ::=  primary "." identifier

primary必须是一个支持属性引用类型的对象,例如模块、列表和实例。接着该对象被要求生成名称为identifier的属性。If this attribute is not available, the exception AttributeError is raised. Otherwise, the type and value of the object produced is determined by the object. Multiple evaluations of the same attribute reference may yield different objects.

5.3.2. Subscriptions

A subscription selects an item of a sequence (string, tuple or list) or mapping (dictionary) object:

subscription ::=  primary "[" expression_list "]"

primary必须是一个序列或者映射类型的对象。

If the primary is a mapping, the expression list must evaluate to an object whose value is one of the keys of the mapping, and the subscription selects the value in the mapping that corresponds to that key. (The expression list is a tuple except if it has exactly one item.)

If the primary is a sequence, the expression (list) must evaluate to a plain integer. If this value is negative, the length of the sequence is added to it (so that, e.g., x[-1] selects the last item of x.) 结果值必须是一个小于序列元素个数的非负整数,下标操作选择索引为该值的元素(从零开始计数)。

A string’s items are characters. A character is not a separate data type but a string of exactly one character.

5.3.3. Slicings

切片选择序列对象(例如,字符串、元组和列表)中一个范围内的元素。切片可以用作表达式或者作为赋值和del语句的目标。The syntax for a slicing:

slicing          ::=  simple_slicing | extended_slicing
simple_slicing   ::=  primary "[" short_slice "]"
extended_slicing ::=  primary "[" slice_list "]"
slice_list       ::=  slice_item ("," slice_item)* [","]
slice_item       ::=  expression | proper_slice | ellipsis
proper_slice     ::=  short_slice | long_slice
short_slice      ::=  [lower_bound] ":" [upper_bound]
long_slice       ::=  short_slice ":" [stride]
lower_bound      ::=  expression
upper_bound      ::=  expression
stride           ::=  expression
ellipsis         ::=  "..."

这里的形式语法有歧义:expession_list看上去也像slice_list,所以任何下标也可以解释为切片。为了不引入更复杂的语法,通过定义在这种情况下解释为下标优先于解释为切片来消除歧义(如果slice_list不包含proper_slice和ellipse也属于这种情况)。类似地,当slice_list只有一个short_slice且没有末尾的逗号时,解释为简单切片要优先于解释为扩展切片。

The semantics for a simple slicing are as follows. primary必须是一个序列对象。The lower and upper bound expressions, if present, must evaluate to plain integers; defaults are zero and the sys.maxint, respectively. If either bound is negative, the sequence’s length is added to it. The slicing now selects all items with index k such that i <= k < j where i and j are the specified lower and upper bounds. This may be an empty sequence. It is not an error if i or j lie outside the range of valid indexes (such items don’t exist so they aren’t selected).

The semantics for an extended slicing are as follows. primary必须是一个映射对象,它以从slice_list构造的键做索引,如下所示。如果slice_list包含至少一个逗号,则键是一个包含slice_item转换的元组;否则,long_slice作为键。slice_item是一个表达式时,转换就是那个表达式。slice_item是ellipsis时,转换为内建的Ellipsis对象。proper_slice的转换是一个切片对象(参阅标准类型的层次),它的startstopstep属性分别是表达式lower_bound、upper_bound和stride的值,没有的表达式用None代替。

5.3.4. Calls

A call calls a callable object (e.g., a function) with a possibly empty series of arguments:

call                 ::=  primary "(" [argument_list [","]
                          | expression genexpr_for] ")"
argument_list        ::=  positional_arguments ["," keyword_arguments]
                            ["," "*" expression] ["," keyword_arguments]
                            ["," "**" expression]
                          | keyword_arguments ["," "*" expression]
                            ["," "**" expression]
                          | "*" expression ["," "*" expression] ["," "**" expression]
                          | "**" expression
positional_arguments ::=  expression ("," expression)*
keyword_arguments    ::=  keyword_item ("," keyword_item)*
keyword_item         ::=  identifier "=" expression

A trailing comma may be present after the positional and keyword arguments but does not affect the semantics.

The primary must evaluate to a callable object (user-defined functions, built-in functions, methods of built-in objects, class objects, methods of class instances, and certain class instances themselves are callable; extensions may define additional callable object types). All argument expressions are evaluated before the call is attempted. Please refer to section Function definitions for the syntax of formal parameter lists.

If keyword arguments are present, they are first converted to positional arguments, as follows. 首先,创建一个没有填充的空位序列用于形参。If there are N positional arguments, they are placed in the first N slots. Next, for each keyword argument, the identifier is used to determine the corresponding slot (if the identifier is the same as the first formal parameter name, the first slot is used, and so on). If the slot is already filled, a TypeError exception is raised. Otherwise, the value of the argument is placed in the slot, filling it (even if the expression is None, it fills the slot). When all arguments have been processed, the slots that are still unfilled are filled with the corresponding default value from the function definition. (Default values are calculated, once, when the function is defined; thus, a mutable object such as a list or dictionary used as default value will be shared by all calls that don’t specify an argument value for the corresponding slot; this should usually be avoided.) If there are any unfilled slots for which no default value is specified, a TypeError exception is raised. Otherwise, the list of filled slots is used as the argument list for the call.

CPython implementation detail: An implementation may provide built-in functions whose positional parameters do not have names, even if they are ‘named’ for the purpose of documentation, and which therefore cannot be supplied by keyword. In CPython, this is the case for functions implemented in C that use PyArg_ParseTuple() to parse their arguments.

If there are more positional arguments than there are formal parameter slots, a TypeError exception is raised, unless a formal parameter using the syntax *identifier is present; in this case, that formal parameter receives a tuple containing the excess positional arguments (or an empty tuple if there were no excess positional arguments).

If any keyword argument does not correspond to a formal parameter name, a TypeError exception is raised, unless a formal parameter using the syntax **identifier is present; in this case, that formal parameter receives a dictionary containing the excess keyword arguments (using the keywords as keys and the argument values as corresponding values), or a (new) empty dictionary if there were no excess keyword arguments.

If the syntax *expression appears in the function call, expression must evaluate to an iterable. Elements from this iterable are treated as if they were additional positional arguments; if there are positional arguments x1, ..., xN, and expression evaluates to a sequence y1, ..., yM, this is equivalent to a call with M+N positional arguments x1, ..., xN, y1, ..., yM.

A consequence of this is that although the *expression syntax may appear after some keyword arguments, it is processed before the keyword arguments (and the **expression argument, if any – see below). So:

>>> def f(a, b):
...  print a, b
...
>>> f(b=1, *(2,))
2 1
>>> f(a=1, *(2,))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: f() got multiple values for keyword argument 'a'
>>> f(1, *(2,))
1 2

It is unusual for both keyword arguments and the *expression syntax to be used in the same call, so in practice this confusion does not arise.

If the syntax **expression appears in the function call, expression must evaluate to a mapping, the contents of which are treated as additional keyword arguments. In the case of a keyword appearing in both expression and as an explicit keyword argument, a TypeError exception is raised.

Formal parameters using the syntax *identifier or **identifier cannot be used as positional argument slots or as keyword argument names. Formal parameters using the syntax (sublist) cannot be used as keyword argument names; the outermost sublist corresponds to a single unnamed argument slot, and the argument value is assigned to the sublist using the usual tuple assignment rules after all other parameter processing is done.

A call always returns some value, possibly None, unless it raises an exception. How this value is computed depends on the type of the callable object.

If it is—

一个用户定义的函数:

The code block for the function is executed, passing it the argument list. The first thing the code block will do is bind the formal parameters to the arguments; this is described in section Function definitions. When the code block executes a return statement, this specifies the return value of the function call.

一个内建的函数或方法:

The result is up to the interpreter; see Built-in Functions for the descriptions of built-in functions and methods.

一个类对象:

A new instance of that class is returned.

一个类实例的方法:

The corresponding user-defined function is called, with an argument list that is one longer than the argument list of the call: the instance becomes the first argument.

一个类实例:

The class must define a __call__() method; the effect is then the same as if that method was called.

5.4. 乘方操作符

乘方操作符的绑定性比它左侧的一元操作符高;比它右侧的一元操作符绑定性低。The syntax is:

power ::=  primary ["**" u_expr]

Thus, in an unparenthesized sequence of power and unary operators, the operators are evaluated from right to left (this does not constrain the evaluation order for the operands): -1**2 results in -1.

The power operator has the same semantics as the built-in pow() function, when called with two arguments: it yields its left argument raised to the power of its right argument. The numeric arguments are first converted to a common type. The result type is that of the arguments after coercion.

With mixed operand types, the coercion rules for binary arithmetic operators apply. For int and long int operands, the result has the same type as the operands (after coercion) unless the second argument is negative; in that case, all arguments are converted to float and a float result is delivered. For example, 10**2 returns 100, but 10**-2 returns 0.01. (This last feature was added in Python 2.2. In Python 2.1 and before, if both arguments were of integer types and the second argument was negative, an exception was raised).

Raising 0.0 to a negative power results in a ZeroDivisionError. Raising a negative number to a fractional power results in a ValueError.

5.5. 一元算术和位操作

All unary arithmetic and bitwise operations have the same priority:

u_expr ::=  power | "-" u_expr | "+" u_expr | "~" u_expr

The unary - (minus) operator yields the negation of its numeric argument.

The unary + (plus) operator yields its numeric argument unchanged.

The unary ~ (invert) operator yields the bitwise inversion of its plain or long integer argument. The bitwise inversion of x is defined as -(x+1). 它只适用于整数数值。

在这三种情形中,如果参数的类型不合适,都将引发TypeError异常。

5.6. 二元算术操作

二元算术操作具有传统的优先级。注意这里的某些操作同样适用于一些非数值类型。除了乘方操作符,有两个优先级,一个针对乘法操作符,一个针对加法操作符:

m_expr ::=  u_expr | m_expr "*" u_expr | m_expr "//" u_expr | m_expr "/" u_expr
            | m_expr "%" u_expr
a_expr ::=  m_expr | a_expr "+" m_expr | a_expr "-" m_expr

*(乘法)操作符产生它参数的乘积。其参数必须都是数值,或者一个是整数(普通整数或长整数)另外一个是序列。在前一种情况下,数值会被转换成一个相同的类型然后一起相乘。在后一种情况下,将进行序列的重复操作;负的重复值将产生一个空的序列。

/(除法)和//(整除)操作符产生它们参数的商。其数值参数首先被转换成相同的类型。普通整数或者长整数除法产生一个相同类型的整数;其结果是在算术除法的结果上调用“取整”函数。除以零将引发ZeroDivisionError异常。

%(取模)操作符产生第一个参数除以第二个参数后的余数。其数值参数将首先被转换成相同的类型。右边的参数为零将引发ZeroDivisionError异常。参数可以是浮点数,例如,3.14%0.7等于0.34(因为3.14等于4*0.7 + 0.34。)取模操作符永远产生与第二个操作符符号相同的结果(或者为零);结果的绝对值将严格小于第二操作数的绝对值[2]

整数的除法和取模操作由下面的等式相关联:x == (x/y)*y + (x%y)整数除法和取模同样由内建函数divmod()相关联:divmod(x, y) == (x/y, x%y)这些等式对于浮点数不成立;类似的等式在x/y替换为floor(x/y)或者floor(x/y) - 1时近似成立[3]

除了执行整数的取模操作,%操作符还被字符串和unicode对象重载用于执行字符串的格式化(也叫做插值)。字符串格式化的语法在Python库参考的字符串格式化一节讲述。

2.3版后废弃的内容:整除操作符、取模操作符和divmod()函数不再为复数定义。作为替代,如果合适,使用将abs()函数将其转换为浮点数。

+(加法)操作符产生其参数的和。其参数必须都是数值或者都是相同类型的序列。在前一种情况下,数值被转换成相同的类型然后一起相加。在后一种情况下,序列被连接在一起。

-(减法)操作符产生其参数差。其参数首先被转换成相同的类型。

5.7. 移位操作

移位操作的优先级低于算术操作:

shift_expr ::=  a_expr | shift_expr ( "<<" | ">>" ) a_expr

这些操作符接收普通整数或者长整数作为参数。参数会被转换成一种共同的类型。它们将第一个参数向左或向右移动第二个参数指出的位数。

右移n位定义为除以pow(2, n)左移n位定义为乘以pow(2, n)负的移位数目会引发ValueError异常。

在当前的实现中,要求右操作数至多为sys.maxsize如果右操作数大于sys.maxsize,将引发OverflowError异常。

5.8. 二元位操作

下面三种位操作具有各自不同的优先级:

and_expr ::=  shift_expr | and_expr "&" shift_expr
xor_expr ::=  and_expr | xor_expr "^" and_expr
or_expr  ::=  xor_expr | or_expr "|" xor_expr

&操作符产生按位与,它的参数必须是普通整数或长整数。参数会被转换成一种共同的类型。

^操作符产生按位异或,它的参数必须是普通整数或长整数。参数会被转换成一种共同的类型。

|操作符产生按位或,它的参数必须是普通整数或长整数。参数会被转换成一种共同的类型。

5.9. Comparisons

Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. Also unlike C, expressions like a < b < c have the interpretation that is conventional in mathematics:

comparison    ::=  or_expr ( comp_operator or_expr )*
comp_operator ::=  "<" | ">" | "==" | ">=" | "<=" | "<>" | "!="
                   | "is" ["not"] | ["not"] "in"

比较操作产生两个值:True 或者False

Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

形式上,如果a, b, c, ..., y, z是表达式且op1, op2, ..., opN是比较操作符,那么a op1 b op2 c ... y opN z等同于a op1 b and b op2 c and ... y opN z,不同点是每个表达式值至多计算一次。

Note that a op1 b op2 c doesn’t imply any kind of comparison between a and c, so that, e.g., x < y > z is perfectly legal (though perhaps not pretty).

The forms <> and != are equivalent; for consistency with C, != is preferred; where != is mentioned below <> is also accepted. The <> spelling is considered obsolescent.

The operators <, >, ==, >=, <=, and != compare the values of two objects. The objects need not have the same type. If both are numbers, they are converted to a common type. Otherwise, objects of different types always compare unequal, and are ordered consistently but arbitrarily. You can control comparison behavior of objects of non-built-in types by defining a __cmp__ method or rich comparison methods like __gt__, described in section Special method names.

(This unusual definition of comparison was used to simplify the definition of operations like sorting and the in and not in operators. In the future, the comparison rules for objects of different types are likely to change.)

Comparison of objects of the same type depends on the type:

  • Numbers are compared arithmetically.

  • Strings are compared lexicographically using the numeric equivalents (the result of the built-in function ord()) of their characters. Unicode and 8-bit strings are fully interoperable in this behavior. [4]

  • Tuples and lists are compared lexicographically using comparison of corresponding elements. This means that to compare equal, each element must compare equal and the two sequences must be of the same type and have the same length.

    If not equal, the sequences are ordered the same as their first differing elements. For example, cmp([1,2,x], [1,2,y]) returns the same as cmp(x,y). If the corresponding element does not exist, the shorter sequence is ordered first (for example, [1,2] < [1,2,3]).

  • Mappings (dictionaries) compare equal if and only if their sorted (key, value) lists compare equal. [5] Outcomes other than equality are resolved consistently, but are not otherwise defined. [6]

  • Most other objects of built-in types compare unequal unless they are the same object; the choice whether one object is considered smaller or larger than another one is made arbitrarily but consistently within one execution of a program.

The operators in and not in test for collection membership. x in s evaluates to true if x is a member of the collection s, and false otherwise. x not in s returns the negation of x in s. The collection membership test has traditionally been bound to sequences; an object is a member of a collection if the collection is a sequence and contains an element equal to that object. However, it make sense for many other object types to support membership tests without being a sequence. In particular, dictionaries (for keys) and sets support membership testing.

For the list and tuple types, x in y is true if and only if there exists an index i such that x == y[i] is true.

For the Unicode and string types, x in y is true if and only if x is a substring of y.

Changed in version 2.3: Previously, x was required to be a string of length 1.

For user-defined classes which define the __contains__() method, x in y is true if and only if y.__contains__(x) is true.

For user-defined classes which do not define __contains__() but do define __iter__(), x in y is true if some value z with x == z is produced while iterating over y. If an exception is raised during the iteration, it is as if in raised that exception.

最后,尝试旧式的迭代协议:如果一个类定义了__getitem__()x in y为真当且仅当有一个非负的整数索引i使得x == y[i], 且更小的索引不会引发IndexError异常。(如果引发了其它异常,则像是in引发了该异常)。

not in操作符定义为取与in相反的真值。

isis not操作测试对象的ID:x is y当且仅当xy是相同的对象时为真。x is not y产生相反的真值。[7]

5.10. Boolean operations

or_test  ::=  and_test | or_test "or" and_test
and_test ::=  not_test | and_test "and" not_test
not_test ::=  comparison | "not" not_test

In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true. (See the __nonzero__() special method for a way to change this.)

The operator not yields True if its argument is false, False otherwise.

The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

(Note that neither and nor or restrict the value and type they return to False and True, but rather return the last evaluated argument. This is sometimes useful, e.g., if s is a string that should be replaced by a default value if it is empty, the expression s or 'foo' yields the desired value. Because not has to invent a value anyway, it does not bother to return a value of the same type as its argument, so e.g., not 'foo' yields False, not '.)

5.11. Conditional Expressions

New in version 2.5.

conditional_expression ::=  or_test ["if" or_test "else" expression]
expression             ::=  conditional_expression | lambda_expr

Conditional expressions (sometimes called a “ternary operator”) have the lowest priority of all Python operations.

表达式x if C else y首先计算条件C而非 x);if C is true, x is evaluated and its value is returned; otherwise, y is evaluated and its value is returned.

See PEP 308 for more details about conditional expressions.

5.12. Lambdas

lambda_expr     ::=  "lambda" [parameter_list]: expression
old_lambda_expr ::=  "lambda" [parameter_list]: old_expression

Lambda expressions (sometimes called lambda forms) have the same syntactic position as expressions. They are a shorthand to create anonymous functions; the expression lambda arguments: expression yields a function object. The unnamed object behaves like a function object defined with

def name(arguments):
    return expression

关于参数列表的语法,请参见函数定义一节。Note that functions created with lambda expressions cannot contain statements.

5.13. 表达式序列

expression_list ::=  expression ( "," expression )* [","]

至少包含一个逗号的表达式序列产生一个元组。The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right.

The trailing comma is required only to create a single tuple (a.k.a. a singleton); it is optional in all other cases. A single expression without a trailing comma doesn’t create a tuple, but rather yields the value of that expression. (To create an empty tuple, use an empty pair of parentheses: ().)

5.14. Evaluation order

Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.

In the following lines, expressions will be evaluated in the arithmetic order of their suffixes:

expr1, expr2, expr3, expr4
(expr1, expr2, expr3, expr4)
{expr1: expr2, expr3: expr4}
expr1 + expr2 * (expr3 - expr4)
expr1(expr2, expr3, *expr4, **expr5)
expr3, expr4 = expr1, expr2

5.15. 操作符优先级

下面的表格总结了Python中操作符的优先级,从最低的优先级(最弱的绑定性)到最高的优先级(最强的绑定性)。在相同单元格中的操作符具有相同的优先级。除非给出明确的语法,否则操作符是二元的。相同单元格中的操作符从左向右结合(比较操作符和测试操作符除外,它们具有相同的优先级且可以从左向右串联起来 — 参见比较操作符 一节;幂运算符也除外,它是从右向左结合)。

操作符 Description
lambda Lambda 表达式
ifelse 条件表达式
or 布尔或
and 布尔与
not x 布尔非
in, not in, is, is not, <, <=, >, >=, <>, !=, == 比较,包括成员测试和身份测试
| 按位的或
^ 按位的异或
& 按位的与
<<, >> 移位
+, - 加法和减法
*, /, //, % 乘法、除法、取余 [8]
+x, -x, ~x 正数、负数、按位取反
** [9]
x[index], x[index:index], x(arguments...), x.attribute 下标、切片、调用、属性引用
(expressions...), [expressions...], {key: value...}, `expressions...` 元组生成、列表生成、字典生成、字符串转换

脚注

[1]在Python 2.3和以后的版本中,列表推导式将它包含的每个for的控制变量“泄露”到包含它的定义域中。然而,这种行为已经弃用,对它的依赖在Python 3中将不能工作。
[2]虽然abs(x%y) < abs(y)在数学上为真,但是由于数字的舍入它对于浮点数可能不为真。例如,假设在一个平台上有一个Python浮点数是IEEE 754双精度数字,为了让-1e-100 %1e100具有与1e100,相同的符号,计算的结果是-1e-100 + 1e100,它在数值上与1e100精确相等。函数math.fmod()返回的结果的符号与第一个参数相匹配,所以在这种情况下返回-1e-100哪种方式更合适取决于应用程序。
[3]如果x非常接近y的一个整数倍,由于舍入floor(x/y) 很可能是一个大于(x-x%y)/y的值。在这些情况下,Python返回后一种结果,以保持divmod(x,y)[0] * y + x % yx非常接近。
[4]虽然Unicode字符串之间在字节级别上比较是讲得通的,但它们对于用户可能不是太直观。例如,字符串u"\u00C7"u"\u0043\u0327"比较起来不同,即使它们都表示相同的Unicode字符(带有变音符号的大写拉丁字母C)。To compare strings in a human recognizable way, compare using unicodedata.normalize().
[5]具体的实现计算这个非常高效,不用构造列表和排序。
[6]早期版本的Python使用字典式的比较排序后的(key, value)列表,但是这对于常见的比较情形非常耗时。更早期的Python版本只比较字典的ID,但是这会导致奇怪的事情,因为人们期望能够通过与{}比较来测试一个字典是否为空。
[7]由于自动垃圾回收、自由列表以及描述器的动态特性,你可能注意到is操作符在某些使用中的不太寻常的行为,比如实例的方法或常数之间的比较。查看它们的文档以获得更多信息。
[8]% 操作符还用于字符串格式化;适用相同的优先级。
[9]乘方操作符**的绑定性弱于它右侧的一元算术和位操作符,就是说,2**-1 is 0.5