8. 复合语句

复合语句是包含其它语句(语句组)的语句;它们会以某种方式影响或控制所包含其它语句的执行。 通常,复合语句会跨越多行,虽然在某些简单形式下整个复合语句也可能包含于一行之内。

if, whilefor 语句用来实现传统的控制流程构造。 try 语句为一组语句指定异常处理和/和清理代码,而 with 语句允许在一个代码块周围执行初始化和终结化代码。 函数和类定义在语法上也属于复合语句。

一条复合语句由一个或多个‘子句’组成。 一个子句则包含一个句头和一个‘句体’。 特定复合语句的子句头都处于相同的缩进层级。 每个子句头以一个作为唯一标识的关键字开始并以一个冒号结束。 子句体是由一个子句控制的一组语句。 子句体可以是在子句头的冒号之后与其同处一行的一条或由分号分隔的多条简单语句,或者也可以是在其之后缩进的一行或多行语句。 只有后一种形式的子句体才能包含嵌套的复合语句;以下形式是不合法的,这主要是因为无法分清某个后续的 else 子句应该属于哪个 if 子句:

if test1: if test2: print(x)

还要注意的是在这种情形下分号的绑定比冒号更紧密,因此在以下示例中,所有 print() 调用或者都不执行,或者都执行:

if x < y < z: print(x); print(y); print(z)

总结:

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | match_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]

请注意语句总是以 NEWLINE 结束,之后可能跟随一个 DEDENT。 还要注意可选的后续子句总是以一个不能作为语句开头的关键字作为开头,因此不会产生歧义(‘悬空的 else’问题在 Python 中是通过要求嵌套的 if 语句必须缩进来解决的)。

为了保证清晰,以下各节中语法规则采用将每个子句都放在单独行中的格式。

8.1. if 语句

if 语句用于有条件的执行:

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]

它通过对表达式逐个求值直至找到一个真值(请参阅 布尔运算 了解真值与假值的定义)在子句体中选择唯一匹配的一个;然后执行该子句体(而且 if 语句的其他部分不会被执行或求值)。 如果所有表达式均为假值,则如果 else 子句体如果存在就会被执行。

8.2. while 语句

while 语句用于在表达式保持为真的情况下重复地执行:

while_stmt ::=  "while" assignment_expression ":" suite
                ["else" ":" suite]

这将重复地检验表达式,并且如果其值为真就执行第一个子句体;如果表达式值为假(这可能在第一次检验时就发生)则如果 else 子句体存在就会被执行并终止循环。

第一个子句体中的 break 语句在执行时将终止循环且不执行 else 子句体。 第一个子句体中的 continue 语句在执行时将跳过子句体中的剩余部分并返回检验表达式。

8.3. for 语句

for 语句用于对序列(例如字符串、元组或列表)或其他可迭代对象中的元素进行迭代:

for_stmt ::=  "for" target_list "in" expression_list ":" suite
              ["else" ":" suite]

表达式列表会被求值一次;它应该产生一个可迭代对象。 系统将为 expression_list 的结果创建一个迭代器,然后将为迭代器所提供的每一项执行一次子句体,具体次序与迭代器的返回顺序一致。 每一项会按标准赋值规则 (参见 赋值语句) 被依次赋值给目标列表,然后子句体将被执行。 当所有项被耗尽时 (这会在序列为空或迭代器引发 StopIteration 异常时立刻发生),else 子句的子句体如果存在将会被执行,并终止循环。

第一个子句体中的 break 语句在执行时将终止循环且不执行 else 子句体。 第一个子句体中的 continue 语句在执行时将跳过子句体中的剩余部分并转往下一项继续执行,或者在没有下一项时转往 else 子句执行。

for 循环会对目标列表中的变量进行赋值。 这将覆盖之前对这些变量的所有赋值,包括在 for 循环体中的赋值:

for i in range(10):
    print(i)
    i = 5             # this will not affect the for-loop
                      # because i will be overwritten with the next
                      # index in the range

目标列表中的名称在循环结束时不会被删除,但如果序列为空,则它们根本不会被循环所赋值。 提示:内置函数 range() 会返回一个可迭代的整数序列,适用于模拟 Pascal 中的 for i := a to b do 这种效果;例如 list(range(3)) 会返回列表 [0, 1, 2]

備註

当序列在循环中被修改时会有一个微妙的问题(这只可能发生于可变序列例如列表中)。 会有一个内部计数器被用来跟踪下一个要使用的项,每次迭代都会使计数器递增。 当计数器值达到序列长度时循环就会终止。 这意味着如果语句体从序列中删除了当前(或之前)的一项,下一项就会被跳过(因为其标号将变成已被处理的当前项的标号)。 类似地,如果语句体在序列当前项的前面插入一个新项,当前项会在循环的下一轮中再次被处理。 这会导致麻烦的程序错误,避免此问题的办法是对整个序列使用切片来创建一个临时副本,例如

for x in a[:]:
    if x < 0: a.remove(x)

8.4. try 语句

try 语句可为一组语句指定异常处理器和/或清理代码:

try_stmt  ::=  try1_stmt | try2_stmt
try1_stmt ::=  "try" ":" suite
               ("except" [expression ["as" identifier]] ":" suite)+
               ["else" ":" suite]
               ["finally" ":" suite]
try2_stmt ::=  "try" ":" suite
               "finally" ":" suite

except 子句指定一个或多个异常处理程序。 当 try 子句中没有发生异常时,没有任何异常处理程序会被执行。 当 try 子句中发生异常时,将启动对异常处理程序的搜索。 此搜索会逐一检查 except 子句直至找到与该异常相匹配的子句。 如果存在无表达式的 except 子句,它必须是最后一个;它将匹配任何异常。 对于带有表达式的 except 子句,该表达式会被求值,如果结果对象与发生的异常“兼容”则该子句将匹配该异常。 如果一个对象是异常对象所属的类或基类,或者是包含兼容该异常的项的元组则两者就是兼容的。

如果没有 except 子句与异常相匹配,则会在周边代码和发起调用栈上继续搜索异常处理器。 1

如果在对 except 子句头中的表达式求值时引发了异常,则原来对处理器的搜索会被取消,并在周边代码和调用栈上启动对新异常的搜索(它会被视作是整个 try 语句所引发的异常)。

当找到一个匹配的 except 子句时,该异常将被赋值给该 except 子句在 as 关键字之后指定的目标,如果存在此关键字的话,并且该 except 子句体将被执行。 所有 except 子句都必须有可执行的子句体。 当到达子句体的末尾时,通常会转向整个 try 语句之后继续执行。 (这意味着如果对于同一异常存在有嵌套的两个处理器,而异常发生于内层处理器的 try 子句中,则外层处理器将不会处理该异常。)

当使用 as 将目标赋值为一个异常时,它将在 except 子句结束时被清除。 这就相当于

except E as N:
    foo

被转写为

except E as N:
    try:
        foo
    finally:
        del N

这意味着异常必须赋值给一个不同的名称才能在 except 子句之后引用它。 异常会被清除是因为在附加了回溯信息的情况下,它们会形成堆栈帧的循环引用,使得所有局部变量保持存活直到发生下一次垃圾回收。

Before an except clause's suite is executed, details about the exception are stored in the sys module and can be accessed via sys.exc_info(). sys.exc_info() returns a 3-tuple consisting of the exception class, the exception instance and a traceback object (see section 标准类型层级结构) identifying the point in the program where the exception occurred. The details about the exception accessed via sys.exc_info() are restored to their previous values when leaving an exception handler:

>>> print(sys.exc_info())
(None, None, None)
>>> try:
...     raise TypeError
... except:
...     print(sys.exc_info())
...     try:
...          raise ValueError
...     except:
...         print(sys.exc_info())
...     print(sys.exc_info())
...
(<class 'TypeError'>, TypeError(), <traceback object at 0x10efad080>)
(<class 'ValueError'>, ValueError(), <traceback object at 0x10efad040>)
(<class 'TypeError'>, TypeError(), <traceback object at 0x10efad080>)
>>> print(sys.exc_info())
(None, None, None)

如果控制流离开 try 子句体时没有引发异常,并且没有执行 return, continuebreak 语句,可选的 else 子句将被执行。 else 语句中的异常不会由之前的 except 子句处理。

如果存在 finally,它将指定一个‘清理’处理程序。 try 子句会被执行,包括任何 exceptelse 子句。 如果在这些子句中发生任何未处理的异常,该异常会被临时保存。 finally 子句将被执行。 如果存在被保存的异常,它会在 finally 子句的末尾被重新引发。 如果 finally 子句引发了另一个异常,被保存的异常会被设为新异常的上下文。 如果 finally 子句执行了 return, breakcontinue 语句,则被保存的异常会被丢弃:

>>> def f():
...     try:
...         1/0
...     finally:
...         return 42
...
>>> f()
42

finally 子句执行期间,程序不能获取异常信息。

return, breakcontinue 语句在一个 try...finally 语句的 try 子语句体中被执行时,finally 子语句也会‘在离开时’被执行。

函数的返回值是由最后被执行的 return 语句所决定的。 由于 finally 子句总是被执行,因此在 finally 子句中被执行的 return 语句总是最后被执行的:

>>> def foo():
...     try:
...         return 'try'
...     finally:
...         return 'finally'
...
>>> foo()
'finally'

有关异常的更多信息可以在 异常 一节找到,有关使用 raise 语句生成异常的信息可以在 raise 语句 一节找到。

3.8 版更變: 在 Python 3.8 之前,continue 语句不允许在 finally 子句中使用,这是因为具体实现存在一个问题。

8.5. with 语句

with 语句用于包装带有使用上下文管理器 (参见 with 语句上下文管理器 一节) 定义的方法的代码块的执行。 这允许对普通的 try...except...finally 使用模式进行封装以方便地重用。

with_stmt          ::=  "with" ( "(" with_stmt_contents ","? ")" | with_stmt_contents ) ":" suite
with_stmt_contents ::=  with_item ("," with_item)*
with_item          ::=  expression ["as" target]

带有一个“项目”的 with 语句的执行过程如下:

  1. 对上下文表达式 (在 with_item 中给出的表达式) 求值以获得一个上下文管理器。

  2. 载入上下文管理器的 __enter__() 以便后续使用。

  3. 载入上下文管理器的 __exit__() 以便后续使用。

  4. 发起调用上下文管理器的 __enter__() 方法。

  5. 如果 with 语句中包含一个目标,来自 __enter__() 的返回值将被赋值给它。

    備註

    with 语句会保证如果 __enter__() 方法返回时未发生错误,则 __exit__() 将总是被调用。 因此,如果在对目标列表赋值期间发生错误,则会将其视为在语句体内部发生的错误。 参见下面的第 6 步。

  6. 执行语句体。

  7. 发起调用上下文管理器的 __exit__() 方法。 如果语句体的退出是由异常导致的,则其类型、值和回溯信息将被作为参数传递给 __exit__()。 否则的话,将提供三个 None 参数。

    如果语句体的退出是由异常导致的,并且来自 __exit__() 方法的返回值为假,则该异常会被重新引发。 如果返回值为真,则该异常会被抑制,并会继续执行 with 语句之后的语句。

    如果语句体由于异常以外的任何原因退出,则来自 __exit__() 的返回值会被忽略,并会在该类退出正常的发生位置继续执行。

以下代码:

with EXPRESSION as TARGET:
    SUITE

在语义上等价于:

manager = (EXPRESSION)
enter = type(manager).__enter__
exit = type(manager).__exit__
value = enter(manager)
hit_except = False

try:
    TARGET = value
    SUITE
except:
    hit_except = True
    if not exit(manager, *sys.exc_info()):
        raise
finally:
    if not hit_except:
        exit(manager, None, None, None)

如果有多个项目,则会视作存在多个 with 语句嵌套来处理多个上下文管理器:

with A() as a, B() as b:
    SUITE

在语义上等价于:

with A() as a:
    with B() as b:
        SUITE

You can also write multi-item context managers in multiple lines if the items are surrounded by parentheses. For example:

with (
    A() as a,
    B() as b,
):
    SUITE

3.1 版更變: 支持多个上下文表达式。

3.10 版更變: Support for using grouping parentheses to break the statement in multiple lines.

也參考

PEP 343 - "with" 语句

Python with 语句的规范描述、背景和示例。

8.6. The match statement

3.10 版新加入.

The match statement is used for pattern matching. Syntax:

match_stmt   ::=  'match' subject_expr ":" NEWLINE INDENT case_block+ DEDENT
subject_expr ::=  star_named_expression "," star_named_expressions?
                  | named_expression
case_block   ::=  'case' patterns [guard] ":" block

備註

This section uses single quotes to denote soft keywords.

Pattern matching takes a pattern as input (following case) and a subject value (following match). The pattern (which may contain subpatterns) is matched against the subject value. The outcomes are:

  • A match success or failure (also termed a pattern success or failure).

  • Possible binding of matched values to a name. The prerequisites for this are further discussed below.

The match and case keywords are soft keywords.

也參考

  • PEP 634 -- Structural Pattern Matching: Specification

  • PEP 636 -- Structural Pattern Matching: Tutorial

8.6.1. 概述

Here's an overview of the logical flow of a match statement:

  1. The subject expression subject_expr is evaluated and a resulting subject value obtained. If the subject expression contains a comma, a tuple is constructed using the standard rules.

  2. Each pattern in a case_block is attempted to match with the subject value. The specific rules for success or failure are described below. The match attempt can also bind some or all of the standalone names within the pattern. The precise pattern binding rules vary per pattern type and are specified below. Name bindings made during a successful pattern match outlive the executed block and can be used after the match statement.

    備註

    During failed pattern matches, some subpatterns may succeed. Do not rely on bindings being made for a failed match. Conversely, do not rely on variables remaining unchanged after a failed match. The exact behavior is dependent on implementation and may vary. This is an intentional decision made to allow different implementations to add optimizations.

  3. If the pattern succeeds, the corresponding guard (if present) is evaluated. In this case all name bindings are guaranteed to have happened.

    • If the guard evaluates as true or is missing, the block inside case_block is executed.

    • Otherwise, the next case_block is attempted as described above.

    • If there are no further case blocks, the match statement is completed.

備註

Users should generally never rely on a pattern being evaluated. Depending on implementation, the interpreter may cache values or use other optimizations which skip repeated evaluations.

A sample match statement:

>>> flag = False
>>> match (100, 200):
...    case (100, 300):  # Mismatch: 200 != 300
...        print('Case 1')
...    case (100, 200) if flag:  # Successful match, but guard fails
...        print('Case 2')
...    case (100, y):  # Matches and binds y to 200
...        print(f'Case 3, y: {y}')
...    case _:  # Pattern not attempted
...        print('Case 4, I match anything!')
...
Case 3, y: 200

In this case, if flag is a guard. Read more about that in the next section.

8.6.2. Guards

guard ::=  "if" named_expression

A guard (which is part of the case) must succeed for code inside the case block to execute. It takes the form: if followed by an expression.

The logical flow of a case block with a guard follows:

  1. Check that the pattern in the case block succeeded. If the pattern failed, the guard is not evaluated and the next case block is checked.

  2. If the pattern succeeded, evaluate the guard.

    • If the guard condition evaluates as true, the case block is selected.

    • If the guard condition evaluates as false, the case block is not selected.

    • If the guard raises an exception during evaluation, the exception bubbles up.

Guards are allowed to have side effects as they are expressions. Guard evaluation must proceed from the first to the last case block, one at a time, skipping case blocks whose pattern(s) don't all succeed. (I.e., guard evaluation must happen in order.) Guard evaluation must stop once a case block is selected.

8.6.3. Irrefutable Case Blocks

An irrefutable case block is a match-all case block. A match statement may have at most one irrefutable case block, and it must be last.

A case block is considered irrefutable if it has no guard and its pattern is irrefutable. A pattern is considered irrefutable if we can prove from its syntax alone that it will always succeed. Only the following patterns are irrefutable:

8.6.4. Patterns

備註

This section uses grammar notations beyond standard EBNF:

  • the notation SEP.RULE+ is shorthand for RULE (SEP RULE)*

  • the notation !RULE is shorthand for a negative lookahead assertion

The top-level syntax for patterns is:

patterns       ::=  open_sequence_pattern | pattern
pattern        ::=  as_pattern | or_pattern
closed_pattern ::=  | literal_pattern
                    | capture_pattern
                    | wildcard_pattern
                    | value_pattern
                    | group_pattern
                    | sequence_pattern
                    | mapping_pattern
                    | class_pattern

The descriptions below will include a description "in simple terms" of what a pattern does for illustration purposes (credits to Raymond Hettinger for a document that inspired most of the descriptions). Note that these descriptions are purely for illustration purposes and may not reflect the underlying implementation. Furthermore, they do not cover all valid forms.

8.6.4.1. OR Patterns

An OR pattern is two or more patterns separated by vertical bars |. Syntax:

or_pattern ::=  "|".closed_pattern+

Only the final subpattern may be irrefutable, and each subpattern must bind the same set of names to avoid ambiguity.

An OR pattern matches each of its subpatterns in turn to the subject value, until one succeeds. The OR pattern is then considered successful. Otherwise, if none of the subpatterns succeed, the OR pattern fails.

In simple terms, P1 | P2 | ... will try to match P1, if it fails it will try to match P2, succeeding immediately if any succeeds, failing otherwise.

8.6.4.2. AS Patterns

An AS pattern matches an OR pattern on the left of the as keyword against a subject. Syntax:

as_pattern ::=  or_pattern "as" capture_pattern

If the OR pattern fails, the AS pattern fails. Otherwise, the AS pattern binds the subject to the name on the right of the as keyword and succeeds. capture_pattern cannot be a a _.

In simple terms P as NAME will match with P, and on success it will set NAME = <subject>.

8.6.4.3. Literal Patterns

A literal pattern corresponds to most literals in Python. Syntax:

literal_pattern ::=  signed_number
                     | signed_number "+" NUMBER
                     | signed_number "-" NUMBER
                     | strings
                     | "None"
                     | "True"
                     | "False"
                     | signed_number: NUMBER | "-" NUMBER

The rule strings and the token NUMBER are defined in the standard Python grammar. Triple-quoted strings are supported. Raw strings and byte strings are supported. 格式字符串字面值 are not supported.

The forms signed_number '+' NUMBER and signed_number '-' NUMBER are for expressing complex numbers; they require a real number on the left and an imaginary number on the right. E.g. 3 + 4j.

In simple terms, LITERAL will succeed only if <subject> == LITERAL. For the singletons None, True and False, the is operator is used.

8.6.4.4. Capture Patterns

A capture pattern binds the subject value to a name. Syntax:

capture_pattern ::=  !'_' NAME

A single underscore _ is not a capture pattern (this is what !'_' expresses). It is instead treated as a wildcard_pattern.

In a given pattern, a given name can only be bound once. E.g. case x, x: ... is invalid while case [x] | x: ... is allowed.

Capture patterns always succeed. The binding follows scoping rules established by the assignment expression operator in PEP 572; the name becomes a local variable in the closest containing function scope unless there's an applicable global or nonlocal statement.

In simple terms NAME will always succeed and it will set NAME = <subject>.

8.6.4.5. Wildcard Patterns

A wildcard pattern always succeeds (matches anything) and binds no name. Syntax:

wildcard_pattern ::=  '_'

_ is a soft keyword within any pattern, but only within patterns. It is an identifier, as usual, even within match subject expressions, guards, and case blocks.

In simple terms, _ will always succeed.

8.6.4.6. Value Patterns

A value pattern represents a named value in Python. Syntax:

value_pattern ::=  attr
attr          ::=  name_or_attr "." NAME
name_or_attr  ::=  attr | NAME

The dotted name in the pattern is looked up using standard Python name resolution rules. The pattern succeeds if the value found compares equal to the subject value (using the == equality operator).

In simple terms NAME1.NAME2 will succeed only if <subject> == NAME1.NAME2

備註

If the same value occurs multiple times in the same match statement, the interpreter may cache the first value found and reuse it rather than repeat the same lookup. This cache is strictly tied to a given execution of a given match statement.

8.6.4.7. Group Patterns

A group pattern allows users to add parentheses around patterns to emphasize the intended grouping. Otherwise, it has no additional syntax. Syntax:

group_pattern ::=  "(" pattern ")"

In simple terms (P) has the same effect as P.

8.6.4.8. Sequence Patterns

A sequence pattern contains several subpatterns to be matched against sequence elements. The syntax is similar to the unpacking of a list or tuple.

sequence_pattern       ::=  "[" [maybe_sequence_pattern] "]"
                            | "(" [open_sequence_pattern] ")"
open_sequence_pattern  ::=  maybe_star_pattern "," [maybe_sequence_pattern]
maybe_sequence_pattern ::=  ",".maybe_star_pattern+ ","?
maybe_star_pattern     ::=  star_pattern | pattern
star_pattern           ::=  "*" (capture_pattern | wildcard_pattern)

There is no difference if parentheses or square brackets are used for sequence patterns (i.e. (...) vs [...] ).

備註

A single pattern enclosed in parentheses without a trailing comma (e.g. (3 | 4)) is a group pattern. While a single pattern enclosed in square brackets (e.g. [3 | 4]) is still a sequence pattern.

At most one star subpattern may be in a sequence pattern. The star subpattern may occur in any position. If no star subpattern is present, the sequence pattern is a fixed-length sequence pattern; otherwise it is a variable-length sequence pattern.

The following is the logical flow for matching a sequence pattern against a subject value:

  1. If the subject value is not a sequence 2, the sequence pattern fails.

  2. If the subject value is an instance of str, bytes or bytearray the sequence pattern fails.

  3. The subsequent steps depend on whether the sequence pattern is fixed or variable-length.

    If the sequence pattern is fixed-length:

    1. If the length of the subject sequence is not equal to the number of subpatterns, the sequence pattern fails

    2. Subpatterns in the sequence pattern are matched to their corresponding items in the subject sequence from left to right. Matching stops as soon as a subpattern fails. If all subpatterns succeed in matching their corresponding item, the sequence pattern succeeds.

    Otherwise, if the sequence pattern is variable-length:

    1. If the length of the subject sequence is less than the number of non-star subpatterns, the sequence pattern fails.

    2. The leading non-star subpatterns are matched to their corresponding items as for fixed-length sequences.

    3. If the previous step succeeds, the star subpattern matches a list formed of the remaining subject items, excluding the remaining items corresponding to non-star subpatterns following the star subpattern.

    4. Remaining non-star subpatterns are matched to their corresponding subject items, as for a fixed-length sequence.

    備註

    The length of the subject sequence is obtained via len() (i.e. via the __len__() protocol). This length may be cached by the interpreter in a similar manner as value patterns.

In simple terms [P1, P2, P3, ... , P<N>] matches only if all the following happens:

  • check <subject> is a sequence

  • len(subject) == <N>

  • P1 matches <subject>[0] (note that this match can also bind names)

  • P2 matches <subject>[1] (note that this match can also bind names)

  • ... and so on for the corresponding pattern/element.

8.6.4.9. Mapping Patterns

A mapping pattern contains one or more key-value patterns. The syntax is similar to the construction of a dictionary. Syntax:

mapping_pattern     ::=  "{" [items_pattern] "}"
items_pattern       ::=  ",".key_value_pattern+ ","?
key_value_pattern   ::=  (literal_pattern | value_pattern) ":" pattern
                         | double_star_pattern
double_star_pattern ::=  "**" capture_pattern

At most one double star pattern may be in a mapping pattern. The double star pattern must be the last subpattern in the mapping pattern.

Duplicate keys in mapping patterns are disallowed. Duplicate literal keys will raise a SyntaxError. Two keys that otherwise have the same value will raise a ValueError at runtime.

The following is the logical flow for matching a mapping pattern against a subject value:

  1. If the subject value is not a mapping 3,the mapping pattern fails.

  2. If every key given in the mapping pattern is present in the subject mapping, and the pattern for each key matches the corresponding item of the subject mapping, the mapping pattern succeeds.

  3. If duplicate keys are detected in the mapping pattern, the pattern is considered invalid. A SyntaxError is raised for duplicate literal values; or a ValueError for named keys of the same value.

備註

Key-value pairs are matched using the two-argument form of the mapping subject's get() method. Matched key-value pairs must already be present in the mapping, and not created on-the-fly via __missing__() or __getitem__().

In simple terms {KEY1: P1, KEY2: P2, ... } matches only if all the following happens:

  • check <subject> is a mapping

  • KEY1 in <subject>

  • P1 matches <subject>[KEY1]

  • ... and so on for the corresponding KEY/pattern pair.

8.6.4.10. Class Patterns

A class pattern represents a class and its positional and keyword arguments (if any). Syntax:

class_pattern       ::=  name_or_attr "(" [pattern_arguments ","?] ")"
pattern_arguments   ::=  positional_patterns ["," keyword_patterns]
                         | keyword_patterns
positional_patterns ::=  ",".pattern+
keyword_patterns    ::=  ",".keyword_pattern+
keyword_pattern     ::=  NAME "=" pattern

The same keyword should not be repeated in class patterns.

The following is the logical flow for matching a class pattern against a subject value:

  1. If name_or_attr is not an instance of the builtin type , raise TypeError.

  2. If the subject value is not an instance of name_or_attr (tested via isinstance()), the class pattern fails.

  3. If no pattern arguments are present, the pattern succeeds. Otherwise, the subsequent steps depend on whether keyword or positional argument patterns are present.

    For a number of built-in types (specified below), a single positional subpattern is accepted which will match the entire subject; for these types keyword patterns also work as for other types.

    If only keyword patterns are present, they are processed as follows, one by one:

    I. The keyword is looked up as an attribute on the subject.

    • If this raises an exception other than AttributeError, the exception bubbles up.

    • If this raises AttributeError, the class pattern has failed.

    • Else, the subpattern associated with the keyword pattern is matched against the subject's attribute value. If this fails, the class pattern fails; if this succeeds, the match proceeds to the next keyword.

    II. If all keyword patterns succeed, the class pattern succeeds.

    If any positional patterns are present, they are converted to keyword patterns using the __match_args__ attribute on the class name_or_attr before matching:

    I. The equivalent of getattr(cls, "__match_args__", ()) is called.

    • If this raises an exception, the exception bubbles up.

    • If the returned value is not a tuple, the conversion fails and TypeError is raised.

    • If there are more positional patterns than len(cls.__match_args__), TypeError is raised.

    • Otherwise, positional pattern i is converted to a keyword pattern using __match_args__[i] as the keyword. __match_args__[i] must be a string; if not TypeError is raised.

    • If there are duplicate keywords, TypeError is raised.

    II. Once all positional patterns have been converted to keyword patterns,

    the match proceeds as if there were only keyword patterns.

    For the following built-in types the handling of positional subpatterns is different:

    These classes accept a single positional argument, and the pattern there is matched against the whole object rather than an attribute. For example int(0|1) matches the value 0, but not the values 0.0 or False.

In simple terms CLS(P1, attr=P2) matches only if the following happens:

  • isinstance(<subject>, CLS)

  • convert P1 to a keyword pattern using CLS.__match_args__

  • For each keyword argument attr=P2:
    • hasattr(<subject>, "attr")

    • P2 matches <subject>.attr

  • ... and so on for the corresponding keyword argument/pattern pair.

也參考

  • PEP 634 -- Structural Pattern Matching: Specification

  • PEP 636 -- Structural Pattern Matching: Tutorial

8.7. 函数定义

函数定义就是对用户自定义函数的定义(参见 标准类型层级结构 一节):

funcdef                   ::=  [decorators] "def" funcname "(" [parameter_list] ")"
                               ["->" expression] ":" suite
decorators                ::=  decorator+
decorator                 ::=  "@" assignment_expression NEWLINE
parameter_list            ::=  defparameter ("," defparameter)* "," "/" ["," [parameter_list_no_posonly]]
                                 | parameter_list_no_posonly
parameter_list_no_posonly ::=  defparameter ("," defparameter)* ["," [parameter_list_starargs]]
                               | parameter_list_starargs
parameter_list_starargs   ::=  "*" [parameter] ("," defparameter)* ["," ["**" parameter [","]]]
                               | "**" parameter [","]
parameter                 ::=  identifier [":" expression]
defparameter              ::=  parameter ["=" expression]
funcname                  ::=  identifier

函数定义是一条可执行语句。 它执行时会在当前局部命名空间中将函数名称绑定到一个函数对象(函数可执行代码的包装器)。 这个函数对象包含对当前全局命名空间的引用,作为函数被调用时所使用的全局命名空间。

函数定义并不会执行函数体;只有当函数被调用时才会执行此操作。 4

一个函数定义可以被一个或多个 decorator 表达式所包装。 当函数被定义时将在包含该函数定义的作用域中对装饰器表达式求值。 求值结果必须是一个可调用对象,它会以该函数对象作为唯一参数被发起调用。 其返回值将被绑定到函数名称而非函数对象。 多个装饰器会以嵌套方式被应用。 例如以下代码

@f1(arg)
@f2
def func(): pass

大致等价于

def func(): pass
func = f1(arg)(f2(func))

不同之处在于原始函数并不会被临时绑定到名称 func

3.9 版更變: 函数可使用任何有效的 assignment_expression 来装饰。 在之前版本中,此语法则更为受限,详情参见 PEP 614

当一个或多个 形参 具有 形参 = 表达式 这样的形式时,该函数就被称为具有“默认形参值”。 对于一个具有默认值的形参,其对应的 argument 可以在调用中被省略,在此情况下会用形参的默认值来替代。 如果一个形参具有默认值,后续所有在 "*" 之前的形参也必须具有默认值 --- 这个句法限制并未在语法中明确表达。

Default parameter values are evaluated from left to right when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that the same "pre-computed" value is used for each call. This is especially important to understand when a default parameter value is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default parameter value is in effect modified. This is generally not what was intended. A way around this is to use None as the default, and explicitly test for it in the body of the function, e.g.:

def whats_on_the_telly(penguin=None):
    if penguin is None:
        penguin = []
    penguin.append("property of the zoo")
    return penguin

函数调用的语义在 调用 一节中有更详细的描述。 函数调用总是会给形参列表中列出的所有形参赋值,或是用位置参数,或是用关键字参数,或是用默认值。 如果存在 "*identifier" 这样的形式,它会被初始化为一个元组来接收任何额外的位置参数,默认为一个空元组。 如果存在 "**identifier" 这样的形式,它会被初始化为一个新的有序映射来接收任何额外的关键字参数,默认为一个相同类型的空映射。 在 "*" 或 "*identifier" 之后的形参都是仅限关键字形参因而只能通过关键字参数传入。 在 "/" 之前的形参都是仅限位置形参因而只能通过位置参数传入。

3.8 版更變: 可以使用 / 函数形参语法来标示仅限位置形参。 请参阅 PEP 570 了解详情。

形参可以带有 标注,其形式为在形参名称后加上 ": expression"。 任何形参都可以带有标注,甚至 *identifier**identifier 这样的形参也可以。 函数可以带有“返回”标注,其形式为在形参列表后加上 "-> expression"。 这些标注可以是任何有效的 Python 表达式。 标注的存在不会改变函数的语义。 标注值可以作为函数对象的 __annotations__ 属性中以对应形参名称为键的字典值被访问。 如果使用了 annotations import from __future__ 的方式,则标注会在运行时保存为字符串以启用延迟求值特性。 否则,它们会在执行函数定义时被求值。 在这种情况下,标注的求值顺序可能与它们在源代码中出现的顺序不同。

创建匿名函数(未绑定到一个名称的函数)以便立即在表达式中使用也是可能的。 这需要使用 lambda 表达式,具体描述见 lambda 表达式 一节。 请注意 lambda 只是简单函数定义的一种简化写法;在 "def" 语句中定义的函数也可以像用 lambda 表达式定义的函数一样被传递或赋值给其他名称。 "def" 形式实际上更为强大,因为它允许执行多条语句和使用标注。

程序员注意事项: 函数属于一类对象。 在一个函数内部执行的 "def" 语句会定义一个局部函数并可被返回或传递。 在嵌套函数中使用的自由变量可以访问包含该 def 语句的函数的局部变量。 详情参见 命名与绑定 一节。

也參考

PEP 3107 - 函数标注

最初的函数标注规范说明。

PEP 484 - 类型提示

标注的标准含意定义:类型提示。

PEP 526 - 变量标注的语法

变量声明的类型提示功能,包括类变量和实例变量

PEP 563 - 延迟的标注求值

支持在运行时通过以字符串形式保存标注而非不是即求值来实现标注内部的向前引用。

8.8. 类定义

类定义就是对类对象的定义 (参见 标准类型层级结构 一节):

classdef    ::=  [decorators] "class" classname [inheritance] ":" suite
inheritance ::=  "(" [argument_list] ")"
classname   ::=  identifier

类定义是一条可执行语句。 其中继承列表通常给出基类的列表 (进阶用法请参见 元类),列表中的每一项都应当被求值为一个允许子类的类对象。 没有继承列表的类默认继承自基类 object;因此,:

class Foo:
    pass

等价于

class Foo(object):
    pass

随后类体将在一个新的执行帧 (参见 命名与绑定) 中被执行,使用新创建的局部命名空间和原有的全局命名空间。 (通常,类体主要包含函数定义。) 当类体结束执行时,其执行帧将被丢弃而其局部命名空间会被保存。 5 一个类对象随后会被创建,其基类使用给定的继承列表,属性字典使用保存的局部命名空间。 类名称将在原有的全局命名空间中绑定到该类对象。

在类体内定义的属性的顺序保存在新类的 __dict__ 中。 请注意此顺序的可靠性只限于类刚被创建时,并且只适用于使用定义语法所定义的类。

类的创建可使用 元类 进行重度定制。

类也可以被装饰:就像装饰函数一样,:

@f1(arg)
@f2
class Foo: pass

大致等价于

class Foo: pass
Foo = f1(arg)(f2(Foo))

装饰器表达式的求值规则与函数装饰器相同。 结果随后会被绑定到类名称。

3.9 版更變: 类可使用任何有效的 assignment_expression 来装饰。 在之前版本中,此语法则更为受限,详情参见 PEP 614

程序员注意事项: 在类定义内定义的变量是类属性;它们将被类实例所共享。 实例属性可通过 self.name = value 在方法中设定。 类和实例属性均可通过 "self.name" 表示法来访问,当通过此方式访问时实例属性会隐藏同名的类属性。 类属性可被用作实例属性的默认值,但在此场景下使用可变值可能导致未预期的结果。 可以使用 描述器 来创建具有不同实现细节的实例变量。

也參考

PEP 3115 - Python 3000 中的元类

将元类声明修改为当前语法的提议,以及关于如何构建带有元类的类的语义描述。

PEP 3129 - 类装饰器

增加类装饰器的提议。 函数和方法装饰器是在 PEP 318 中被引入的。

8.9. 协程

3.5 版新加入.

8.9.1. 协程函数定义

async_funcdef ::=  [decorators] "async" "def" funcname "(" [parameter_list] ")"
                   ["->" expression] ":" suite

Execution of Python coroutines can be suspended and resumed at many points (see coroutine). await expressions, async for and async with can only be used in the body of a coroutine function.

使用 async def 语法定义的函数总是为协程函数,即使它们不包含 awaitasync 关键字。

在协程函数体中使用 yield from 表达式将引发 SyntaxError

协程函数的例子:

async def func(param1, param2):
    do_stuff()
    await some_coroutine()

3.7 版更變: await and async are now keywords; previously they were only treated as such inside the body of a coroutine function.

8.9.2. async for 语句

async_for_stmt ::=  "async" for_stmt

asynchronous iterable 提供了 __aiter__ 方法,该方法会直接返回 asynchronous iterator,它可以在其 __anext__ 方法中调用异步代码。

async for 语句允许方便地对异步可迭代对象进行迭代。

以下代码:

async for TARGET in ITER:
    SUITE
else:
    SUITE2

在语义上等价于:

iter = (ITER)
iter = type(iter).__aiter__(iter)
running = True

while running:
    try:
        TARGET = await type(iter).__anext__(iter)
    except StopAsyncIteration:
        running = False
    else:
        SUITE
else:
    SUITE2

另请参阅 __aiter__()__anext__() 了解详情。

在协程函数体之外使用 async for 语句将引发 SyntaxError

8.9.3. async with 语句

async_with_stmt ::=  "async" with_stmt

asynchronous context manager 是一种 context manager,能够在其 enterexit 方法中暂停执行。

以下代码:

async with EXPRESSION as TARGET:
    SUITE

在语义上等价于:

manager = (EXPRESSION)
aenter = type(manager).__aenter__
aexit = type(manager).__aexit__
value = await aenter(manager)
hit_except = False

try:
    TARGET = value
    SUITE
except:
    hit_except = True
    if not await aexit(manager, *sys.exc_info()):
        raise
finally:
    if not hit_except:
        await aexit(manager, None, None, None)

另请参阅 __aenter__()__aexit__() 了解详情。

在协程函数体之外使用 async with 语句将引发 SyntaxError

也參考

PEP 492 - 使用 async 和 await 语法实现协程

将协程作为 Python 中的一个正式的单独概念,并增加相应的支持语法。

註解

1

异常会被传播给发起调用栈,除非存在一个 finally 子句正好引发了另一个异常。 新引发的异常将导致旧异常的丢失。

2

In pattern matching, a sequence is defined as one of the following:

The following standard library classes are sequences:

備註

Subject values of type str, bytes, and bytearray do not match sequence patterns.

3

In pattern matching, a mapping is defined as one of the following:

The standard library classes dict and types.MappingProxyType are mappings.

4

作为函数体的第一条语句出现的字符串字面值会被转换为函数的 __doc__ 属性,也就是该函数的 docstring

5

作为类体的第一条语句出现的字符串字面值会被转换为命名空间的 __doc__ 条目,也就是该类的 docstring