| 1 |
.. _compound: |
| 2 |
|
| 3 |
******************* |
| 4 |
Compound statements |
| 5 |
******************* |
| 6 |
|
| 7 |
.. index:: pair: compound; statement |
| 8 |
|
| 9 |
Compound statements contain (groups of) other statements; they affect or control |
| 10 |
the execution of those other statements in some way. In general, compound |
| 11 |
statements span multiple lines, although in simple incarnations a whole compound |
| 12 |
statement may be contained in one line. |
| 13 |
|
| 14 |
The :keyword:`if`, :keyword:`while` and :keyword:`for` statements implement |
| 15 |
traditional control flow constructs. :keyword:`try` specifies exception |
| 16 |
handlers and/or cleanup code for a group of statements, while the |
| 17 |
:keyword:`with` statement allows the execution of initialization and |
| 18 |
finalization code around a block of code. Function and class definitions are |
| 19 |
also syntactically compound statements. |
| 20 |
|
| 21 |
.. index:: |
| 22 |
single: clause |
| 23 |
single: suite |
| 24 |
single: ; (semicolon) |
| 25 |
|
| 26 |
A compound statement consists of one or more 'clauses.' A clause consists of a |
| 27 |
header and a 'suite.' The clause headers of a particular compound statement are |
| 28 |
all at the same indentation level. Each clause header begins with a uniquely |
| 29 |
identifying keyword and ends with a colon. A suite is a group of statements |
| 30 |
controlled by a clause. A suite can be one or more semicolon-separated simple |
| 31 |
statements on the same line as the header, following the header's colon, or it |
| 32 |
can be one or more indented statements on subsequent lines. Only the latter |
| 33 |
form of a suite can contain nested compound statements; the following is illegal, |
| 34 |
mostly because it wouldn't be clear to which :keyword:`if` clause a following |
| 35 |
:keyword:`else` clause would belong:: |
| 36 |
|
| 37 |
if test1: if test2: print(x) |
| 38 |
|
| 39 |
Also note that the semicolon binds tighter than the colon in this context, so |
| 40 |
that in the following example, either all or none of the :func:`print` calls are |
| 41 |
executed:: |
| 42 |
|
| 43 |
if x < y < z: print(x); print(y); print(z) |
| 44 |
|
| 45 |
Summarizing: |
| 46 |
|
| 47 |
|
| 48 |
.. productionlist:: python-grammar |
| 49 |
compound_stmt: `if_stmt` |
| 50 |
: | `while_stmt` |
| 51 |
: | `for_stmt` |
| 52 |
: | `try_stmt` |
| 53 |
: | `with_stmt` |
| 54 |
: | `match_stmt` |
| 55 |
: | `funcdef` |
| 56 |
: | `classdef` |
| 57 |
: | `async_with_stmt` |
| 58 |
: | `async_for_stmt` |
| 59 |
: | `async_funcdef` |
| 60 |
suite: `stmt_list` NEWLINE | NEWLINE INDENT `statement`+ DEDENT |
| 61 |
statement: `stmt_list` NEWLINE | `compound_stmt` |
| 62 |
stmt_list: `simple_stmt` (";" `simple_stmt`)* [";"] |
| 63 |
|
| 64 |
.. index:: |
| 65 |
single: NEWLINE token |
| 66 |
single: DEDENT token |
| 67 |
pair: dangling; else |
| 68 |
|
| 69 |
Note that statements always end in a ``NEWLINE`` possibly followed by a |
| 70 |
``DEDENT``. Also note that optional continuation clauses always begin with a |
| 71 |
keyword that cannot start a statement, thus there are no ambiguities (the |
| 72 |
'dangling :keyword:`else`' problem is solved in Python by requiring nested |
| 73 |
:keyword:`if` statements to be indented). |
| 74 |
|
| 75 |
The formatting of the grammar rules in the following sections places each clause |
| 76 |
on a separate line for clarity. |
| 77 |
|
| 78 |
|
| 79 |
.. _if: |
| 80 |
.. _elif: |
| 81 |
.. _else: |
| 82 |
|
| 83 |
The :keyword:`!if` statement |
| 84 |
============================ |
| 85 |
|
| 86 |
.. index:: |
| 87 |
! pair: statement; if |
| 88 |
pair: keyword; elif |
| 89 |
pair: keyword; else |
| 90 |
single: : (colon); compound statement |
| 91 |
|
| 92 |
The :keyword:`if` statement is used for conditional execution: |
| 93 |
|
| 94 |
.. productionlist:: python-grammar |
| 95 |
if_stmt: "if" `assignment_expression` ":" `suite` |
| 96 |
: ("elif" `assignment_expression` ":" `suite`)* |
| 97 |
: ["else" ":" `suite`] |
| 98 |
|
| 99 |
It selects exactly one of the suites by evaluating the expressions one by one |
| 100 |
until one is found to be true (see section :ref:`booleans` for the definition of |
| 101 |
true and false); then that suite is executed (and no other part of the |
| 102 |
:keyword:`if` statement is executed or evaluated). If all expressions are |
| 103 |
false, the suite of the :keyword:`else` clause, if present, is executed. |
| 104 |
|
| 105 |
|
| 106 |
.. _while: |
| 107 |
|
| 108 |
The :keyword:`!while` statement |
| 109 |
=============================== |
| 110 |
|
| 111 |
.. index:: |
| 112 |
! pair: statement; while |
| 113 |
pair: keyword; else |
| 114 |
pair: loop; statement |
| 115 |
single: : (colon); compound statement |
| 116 |
|
| 117 |
The :keyword:`while` statement is used for repeated execution as long as an |
| 118 |
expression is true: |
| 119 |
|
| 120 |
.. productionlist:: python-grammar |
| 121 |
while_stmt: "while" `assignment_expression` ":" `suite` |
| 122 |
: ["else" ":" `suite`] |
| 123 |
|
| 124 |
This repeatedly tests the expression and, if it is true, executes the first |
| 125 |
suite; if the expression is false (which may be the first time it is tested) the |
| 126 |
suite of the :keyword:`!else` clause, if present, is executed and the loop |
| 127 |
terminates. |
| 128 |
|
| 129 |
.. index:: |
| 130 |
pair: statement; break |
| 131 |
pair: statement; continue |
| 132 |
|
| 133 |
A :keyword:`break` statement executed in the first suite terminates the loop |
| 134 |
without executing the :keyword:`!else` clause's suite. A :keyword:`continue` |
| 135 |
statement executed in the first suite skips the rest of the suite and goes back |
| 136 |
to testing the expression. |
| 137 |
|
| 138 |
|
| 139 |
.. _for: |
| 140 |
|
| 141 |
The :keyword:`!for` statement |
| 142 |
============================= |
| 143 |
|
| 144 |
.. index:: |
| 145 |
! pair: statement; for |
| 146 |
pair: keyword; in |
| 147 |
pair: keyword; else |
| 148 |
pair: target; list |
| 149 |
pair: loop; statement |
| 150 |
pair: object; sequence |
| 151 |
single: : (colon); compound statement |
| 152 |
|
| 153 |
The :keyword:`for` statement is used to iterate over the elements of a sequence |
| 154 |
(such as a string, tuple or list) or other iterable object: |
| 155 |
|
| 156 |
.. productionlist:: python-grammar |
| 157 |
for_stmt: "for" `target_list` "in" `starred_expression_list` ":" `suite` |
| 158 |
: ["else" ":" `suite`] |
| 159 |
|
| 160 |
The :token:`~python-grammar:starred_expression_list` expression is evaluated |
| 161 |
once; it should yield an :term:`iterable` object. An :term:`iterator` is |
| 162 |
created for that iterable. The first item provided by the iterator is then |
| 163 |
assigned to the target list using the standard rules for assignments |
| 164 |
(see :ref:`assignment`), and the suite is executed. This repeats for each |
| 165 |
item provided by the iterator. When the iterator is exhausted, |
| 166 |
the suite in the :keyword:`!else` clause, |
| 167 |
if present, is executed, and the loop terminates. |
| 168 |
|
| 169 |
.. index:: |
| 170 |
pair: statement; break |
| 171 |
pair: statement; continue |
| 172 |
|
| 173 |
A :keyword:`break` statement executed in the first suite terminates the loop |
| 174 |
without executing the :keyword:`!else` clause's suite. A :keyword:`continue` |
| 175 |
statement executed in the first suite skips the rest of the suite and continues |
| 176 |
with the next item, or with the :keyword:`!else` clause if there is no next |
| 177 |
item. |
| 178 |
|
| 179 |
The for-loop makes assignments to the variables in the target list. |
| 180 |
This overwrites all previous assignments to those variables including |
| 181 |
those made in the suite of the for-loop:: |
| 182 |
|
| 183 |
for i in range(10): |
| 184 |
print(i) |
| 185 |
i = 5 # this will not affect the for-loop |
| 186 |
# because i will be overwritten with the next |
| 187 |
# index in the range |
| 188 |
|
| 189 |
|
| 190 |
.. index:: |
| 191 |
pair: built-in function; range |
| 192 |
|
| 193 |
Names in the target list are not deleted when the loop is finished, but if the |
| 194 |
sequence is empty, they will not have been assigned to at all by the loop. Hint: |
| 195 |
the built-in type :func:`range` represents immutable arithmetic sequences of integers. |
| 196 |
For instance, iterating ``range(3)`` successively yields 0, 1, and then 2. |
| 197 |
|
| 198 |
.. versionchanged:: 3.11 |
| 199 |
Starred elements are now allowed in the expression list. |
| 200 |
|
| 201 |
|
| 202 |
.. _try: |
| 203 |
|
| 204 |
The :keyword:`!try` statement |
| 205 |
============================= |
| 206 |
|
| 207 |
.. index:: |
| 208 |
! pair: statement; try |
| 209 |
pair: keyword; except |
| 210 |
pair: keyword; finally |
| 211 |
pair: keyword; else |
| 212 |
pair: keyword; as |
| 213 |
single: : (colon); compound statement |
| 214 |
|
| 215 |
The :keyword:`!try` statement specifies exception handlers and/or cleanup code |
| 216 |
for a group of statements: |
| 217 |
|
| 218 |
.. productionlist:: python-grammar |
| 219 |
try_stmt: `try1_stmt` | `try2_stmt` | `try3_stmt` |
| 220 |
try1_stmt: "try" ":" `suite` |
| 221 |
: ("except" [`expression` ["as" `identifier`]] ":" `suite`)+ |
| 222 |
: ["else" ":" `suite`] |
| 223 |
: ["finally" ":" `suite`] |
| 224 |
try2_stmt: "try" ":" `suite` |
| 225 |
: ("except" "*" `expression` ["as" `identifier`] ":" `suite`)+ |
| 226 |
: ["else" ":" `suite`] |
| 227 |
: ["finally" ":" `suite`] |
| 228 |
try3_stmt: "try" ":" `suite` |
| 229 |
: "finally" ":" `suite` |
| 230 |
|
| 231 |
Additional information on exceptions can be found in section :ref:`exceptions`, |
| 232 |
and information on using the :keyword:`raise` statement to generate exceptions |
| 233 |
may be found in section :ref:`raise`. |
| 234 |
|
| 235 |
.. versionchanged:: 3.14 |
| 236 |
Support for optionally dropping grouping parentheses when using multiple exception types. See :pep:`758`. |
| 237 |
|
| 238 |
.. _except: |
| 239 |
|
| 240 |
:keyword:`!except` clause |
| 241 |
------------------------- |
| 242 |
|
| 243 |
The :keyword:`!except` clause(s) specify one or more exception handlers. When no |
| 244 |
exception occurs in the :keyword:`try` clause, no exception handler is executed. |
| 245 |
When an exception occurs in the :keyword:`!try` suite, a search for an exception |
| 246 |
handler is started. This search inspects the :keyword:`!except` clauses in turn |
| 247 |
until one is found that matches the exception. |
| 248 |
An expression-less :keyword:`!except` clause, if present, must be last; |
| 249 |
it matches any exception. |
| 250 |
|
| 251 |
For an :keyword:`!except` clause with an expression, the |
| 252 |
expression must evaluate to an exception type or a tuple of exception types. Parentheses |
| 253 |
can be dropped if multiple exception types are provided and the ``as`` clause is not used. |
| 254 |
The raised exception matches an :keyword:`!except` clause whose expression evaluates |
| 255 |
to the class or a :term:`non-virtual base class <abstract base class>` of the exception object, |
| 256 |
or to a tuple that contains such a class. |
| 257 |
|
| 258 |
If no :keyword:`!except` clause matches the exception, |
| 259 |
the search for an exception handler |
| 260 |
continues in the surrounding code and on the invocation stack. [#]_ |
| 261 |
|
| 262 |
If the evaluation of an expression |
| 263 |
in the header of an :keyword:`!except` clause raises an exception, |
| 264 |
the original search for a handler is canceled and a search starts for |
| 265 |
the new exception in the surrounding code and on the call stack (it is treated |
| 266 |
as if the entire :keyword:`try` statement raised the exception). |
| 267 |
|
| 268 |
.. index:: single: as; except clause |
| 269 |
|
| 270 |
When a matching :keyword:`!except` clause is found, |
| 271 |
the exception is assigned to the target |
| 272 |
specified after the :keyword:`!as` keyword in that :keyword:`!except` clause, |
| 273 |
if present, and the :keyword:`!except` clause's suite is executed. |
| 274 |
All :keyword:`!except` clauses must have an executable block. |
| 275 |
When the end of this block is reached, execution continues |
| 276 |
normally after the entire :keyword:`try` statement. |
| 277 |
(This means that if two nested handlers exist for the same exception, |
| 278 |
and the exception occurs in the :keyword:`!try` clause of the inner handler, |
| 279 |
the outer handler will not handle the exception.) |
| 280 |
|
| 281 |
When an exception has been assigned using ``as target``, it is cleared at the |
| 282 |
end of the :keyword:`!except` clause. This is as if :: |
| 283 |
|
| 284 |
except E as N: |
| 285 |
foo |
| 286 |
|
| 287 |
was translated to :: |
| 288 |
|
| 289 |
except E as N: |
| 290 |
try: |
| 291 |
foo |
| 292 |
finally: |
| 293 |
del N |
| 294 |
|
| 295 |
This means the exception must be assigned to a different name to be able to |
| 296 |
refer to it after the :keyword:`!except` clause. |
| 297 |
Exceptions are cleared because with the |
| 298 |
traceback attached to them, they form a reference cycle with the stack frame, |
| 299 |
keeping all locals in that frame alive until the next garbage collection occurs. |
| 300 |
|
| 301 |
.. index:: |
| 302 |
pair: module; sys |
| 303 |
pair: object; traceback |
| 304 |
|
| 305 |
Before an :keyword:`!except` clause's suite is executed, |
| 306 |
the exception is stored in the :mod:`sys` module, where it can be accessed |
| 307 |
from within the body of the :keyword:`!except` clause by calling |
| 308 |
:func:`sys.exception`. When leaving an exception handler, the exception |
| 309 |
stored in the :mod:`sys` module is reset to its previous value:: |
| 310 |
|
| 311 |
>>> print(sys.exception()) |
| 312 |
None |
| 313 |
>>> try: |
| 314 |
... raise TypeError |
| 315 |
... except: |
| 316 |
... print(repr(sys.exception())) |
| 317 |
... try: |
| 318 |
... raise ValueError |
| 319 |
... except: |
| 320 |
... print(repr(sys.exception())) |
| 321 |
... print(repr(sys.exception())) |
| 322 |
... |
| 323 |
TypeError() |
| 324 |
ValueError() |
| 325 |
TypeError() |
| 326 |
>>> print(sys.exception()) |
| 327 |
None |
| 328 |
|
| 329 |
|
| 330 |
.. index:: |
| 331 |
pair: keyword; except_star |
| 332 |
|
| 333 |
.. _except_star: |
| 334 |
|
| 335 |
:keyword:`!except*` clause |
| 336 |
-------------------------- |
| 337 |
|
| 338 |
The :keyword:`!except*` clause(s) specify one or more handlers for groups of |
| 339 |
exceptions (:exc:`BaseExceptionGroup` instances). A :keyword:`try` statement |
| 340 |
can have either :keyword:`except` or :keyword:`!except*` clauses, but not both. |
| 341 |
The exception type for matching is mandatory in the case of :keyword:`!except*`, |
| 342 |
so ``except*:`` is a syntax error. The type is interpreted as in the case of |
| 343 |
:keyword:`!except`, but matching is performed on the exceptions contained in the |
| 344 |
group that is being handled. An :exc:`TypeError` is raised if a matching |
| 345 |
type is a subclass of :exc:`!BaseExceptionGroup`, because that would have |
| 346 |
ambiguous semantics. |
| 347 |
|
| 348 |
When an exception group is raised in the try block, each :keyword:`!except*` |
| 349 |
clause splits (see :meth:`~BaseExceptionGroup.split`) it into the subgroups |
| 350 |
of matching and non-matching exceptions. If the matching subgroup is not empty, |
| 351 |
it becomes the handled exception (the value returned from :func:`sys.exception`) |
| 352 |
and assigned to the target of the :keyword:`!except*` clause (if there is one). |
| 353 |
Then, the body of the :keyword:`!except*` clause executes. If the non-matching |
| 354 |
subgroup is not empty, it is processed by the next :keyword:`!except*` in the |
| 355 |
same manner. This continues until all exceptions in the group have been matched, |
| 356 |
or the last :keyword:`!except*` clause has run. |
| 357 |
|
| 358 |
After all :keyword:`!except*` clauses execute, the group of unhandled exceptions |
| 359 |
is merged with any exceptions that were raised or re-raised from within |
| 360 |
:keyword:`!except*` clauses. This merged exception group propagates on.:: |
| 361 |
|
| 362 |
>>> try: |
| 363 |
... raise ExceptionGroup("eg", |
| 364 |
... [ValueError(1), TypeError(2), OSError(3), OSError(4)]) |
| 365 |
... except* TypeError as e: |
| 366 |
... print(f'caught {type(e)} with nested {e.exceptions}') |
| 367 |
... except* OSError as e: |
| 368 |
... print(f'caught {type(e)} with nested {e.exceptions}') |
| 369 |
... |
| 370 |
caught <class 'ExceptionGroup'> with nested (TypeError(2),) |
| 371 |
caught <class 'ExceptionGroup'> with nested (OSError(3), OSError(4)) |
| 372 |
+ Exception Group Traceback (most recent call last): |
| 373 |
| File "<doctest default[0]>", line 2, in <module> |
| 374 |
| raise ExceptionGroup("eg", |
| 375 |
| [ValueError(1), TypeError(2), OSError(3), OSError(4)]) |
| 376 |
| ExceptionGroup: eg (1 sub-exception) |
| 377 |
+-+---------------- 1 ---------------- |
| 378 |
| ValueError: 1 |
| 379 |
+------------------------------------ |
| 380 |
|
| 381 |
If the exception raised from the :keyword:`try` block is not an exception group |
| 382 |
and its type matches one of the :keyword:`!except*` clauses, it is caught and |
| 383 |
wrapped by an exception group with an empty message string. This ensures that the |
| 384 |
type of the target ``e`` is consistently :exc:`BaseExceptionGroup`:: |
| 385 |
|
| 386 |
>>> try: |
| 387 |
... raise BlockingIOError |
| 388 |
... except* BlockingIOError as e: |
| 389 |
... print(repr(e)) |
| 390 |
... |
| 391 |
ExceptionGroup('', (BlockingIOError(),)) |
| 392 |
|
| 393 |
:keyword:`break`, :keyword:`continue` and :keyword:`return` |
| 394 |
cannot appear in an :keyword:`!except*` clause. |
| 395 |
|
| 396 |
|
| 397 |
.. index:: |
| 398 |
pair: keyword; else |
| 399 |
pair: statement; return |
| 400 |
pair: statement; break |
| 401 |
pair: statement; continue |
| 402 |
|
| 403 |
.. _except_else: |
| 404 |
|
| 405 |
:keyword:`!else` clause |
| 406 |
----------------------- |
| 407 |
|
| 408 |
The optional :keyword:`!else` clause is executed if the control flow leaves the |
| 409 |
:keyword:`try` suite, no exception was raised, and no :keyword:`return`, |
| 410 |
:keyword:`continue`, or :keyword:`break` statement was executed. Exceptions in |
| 411 |
the :keyword:`!else` clause are not handled by the preceding :keyword:`except` |
| 412 |
clauses. |
| 413 |
|
| 414 |
|
| 415 |
.. index:: pair: keyword; finally |
| 416 |
|
| 417 |
.. _finally: |
| 418 |
|
| 419 |
:keyword:`!finally` clause |
| 420 |
-------------------------- |
| 421 |
|
| 422 |
If :keyword:`!finally` is present, it specifies a 'cleanup' handler. The |
| 423 |
:keyword:`try` clause is executed, including any :keyword:`except` |
| 424 |
and :keyword:`else <except_else>` clauses. |
| 425 |
If an exception occurs in any of the clauses and is not handled, |
| 426 |
the exception is temporarily saved. |
| 427 |
The :keyword:`!finally` clause is executed. If there is a saved exception |
| 428 |
it is re-raised at the end of the :keyword:`!finally` clause. |
| 429 |
If the :keyword:`!finally` clause raises another exception, the saved exception |
| 430 |
is set as the context of the new exception. |
| 431 |
If the :keyword:`!finally` clause executes a :keyword:`return`, :keyword:`break` |
| 432 |
or :keyword:`continue` statement, the saved exception is discarded. For example, |
| 433 |
this function returns 42. |
| 434 |
|
| 435 |
.. code-block:: |
| 436 |
|
| 437 |
def f(): |
| 438 |
try: |
| 439 |
1/0 |
| 440 |
finally: |
| 441 |
return 42 |
| 442 |
|
| 443 |
The exception information is not available to the program during execution of |
| 444 |
the :keyword:`!finally` clause. |
| 445 |
|
| 446 |
.. index:: |
| 447 |
pair: statement; return |
| 448 |
pair: statement; break |
| 449 |
pair: statement; continue |
| 450 |
|
| 451 |
When a :keyword:`return`, :keyword:`break` or :keyword:`continue` statement is |
| 452 |
executed in the :keyword:`try` suite of a :keyword:`!try`...\ :keyword:`!finally` |
| 453 |
statement, the :keyword:`!finally` clause is also executed 'on the way out.' |
| 454 |
|
| 455 |
The return value of a function is determined by the last :keyword:`return` |
| 456 |
statement executed. Since the :keyword:`!finally` clause always executes, a |
| 457 |
:keyword:`!return` statement executed in the :keyword:`!finally` clause will |
| 458 |
always be the last one executed. The following function returns 'finally'. |
| 459 |
|
| 460 |
.. code-block:: |
| 461 |
|
| 462 |
def foo(): |
| 463 |
try: |
| 464 |
return 'try' |
| 465 |
finally: |
| 466 |
return 'finally' |
| 467 |
|
| 468 |
.. versionchanged:: 3.8 |
| 469 |
Prior to Python 3.8, a :keyword:`continue` statement was illegal in the |
| 470 |
:keyword:`!finally` clause due to a problem with the implementation. |
| 471 |
|
| 472 |
.. versionchanged:: 3.14 |
| 473 |
The compiler emits a :exc:`SyntaxWarning` when a :keyword:`return`, |
| 474 |
:keyword:`break` or :keyword:`continue` appears in a :keyword:`!finally` |
| 475 |
block (see :pep:`765`). |
| 476 |
|
| 477 |
|
| 478 |
.. _with: |
| 479 |
.. _as: |
| 480 |
|
| 481 |
The :keyword:`!with` statement |
| 482 |
============================== |
| 483 |
|
| 484 |
.. index:: |
| 485 |
! pair: statement; with |
| 486 |
pair: keyword; as |
| 487 |
single: as; with statement |
| 488 |
single: , (comma); with statement |
| 489 |
single: : (colon); compound statement |
| 490 |
|
| 491 |
The :keyword:`with` statement is used to wrap the execution of a block with |
| 492 |
methods defined by a context manager (see section :ref:`context-managers`). |
| 493 |
This allows common :keyword:`try`...\ :keyword:`except`...\ :keyword:`finally` |
| 494 |
usage patterns to be encapsulated for convenient reuse. |
| 495 |
|
| 496 |
.. productionlist:: python-grammar |
| 497 |
with_stmt: "with" ( "(" `with_stmt_contents` ","? ")" | `with_stmt_contents` ) ":" `suite` |
| 498 |
with_stmt_contents: `with_item` ("," `with_item`)* |
| 499 |
with_item: `expression` ["as" `target`] |
| 500 |
|
| 501 |
The execution of the :keyword:`with` statement with one "item" proceeds as follows: |
| 502 |
|
| 503 |
#. The context expression (the expression given in the |
| 504 |
:token:`~python-grammar:with_item`) is evaluated to obtain a context manager. |
| 505 |
|
| 506 |
#. The context manager's :meth:`~object.__enter__` is loaded for later use. |
| 507 |
|
| 508 |
#. The context manager's :meth:`~object.__exit__` is loaded for later use. |
| 509 |
|
| 510 |
#. The context manager's :meth:`~object.__enter__` method is invoked. |
| 511 |
|
| 512 |
#. If a target was included in the :keyword:`with` statement, the return value |
| 513 |
from :meth:`~object.__enter__` is assigned to it. |
| 514 |
|
| 515 |
.. note:: |
| 516 |
|
| 517 |
The :keyword:`with` statement guarantees that if the :meth:`~object.__enter__` |
| 518 |
method returns without an error, then :meth:`~object.__exit__` will always be |
| 519 |
called. Thus, if an error occurs during the assignment to the target list, |
| 520 |
it will be treated the same as an error occurring within the suite would |
| 521 |
be. See step 7 below. |
| 522 |
|
| 523 |
#. The suite is executed. |
| 524 |
|
| 525 |
#. The context manager's :meth:`~object.__exit__` method is invoked. If an exception |
| 526 |
caused the suite to be exited, its type, value, and traceback are passed as |
| 527 |
arguments to :meth:`~object.__exit__`. Otherwise, three :const:`None` arguments are |
| 528 |
supplied. |
| 529 |
|
| 530 |
If the suite was exited due to an exception, and the return value from the |
| 531 |
:meth:`~object.__exit__` method was false, the exception is reraised. If the return |
| 532 |
value was true, the exception is suppressed, and execution continues with the |
| 533 |
statement following the :keyword:`with` statement. |
| 534 |
|
| 535 |
If the suite was exited for any reason other than an exception, the return |
| 536 |
value from :meth:`~object.__exit__` is ignored, and execution proceeds at the normal |
| 537 |
location for the kind of exit that was taken. |
| 538 |
|
| 539 |
The following code:: |
| 540 |
|
| 541 |
with EXPRESSION as TARGET: |
| 542 |
SUITE |
| 543 |
|
| 544 |
is semantically equivalent to:: |
| 545 |
|
| 546 |
manager = (EXPRESSION) |
| 547 |
enter = manager.__enter__ |
| 548 |
exit = manager.__exit__ |
| 549 |
value = enter() |
| 550 |
hit_except = False |
| 551 |
|
| 552 |
try: |
| 553 |
TARGET = value |
| 554 |
SUITE |
| 555 |
except: |
| 556 |
hit_except = True |
| 557 |
if not exit(*sys.exc_info()): |
| 558 |
raise |
| 559 |
finally: |
| 560 |
if not hit_except: |
| 561 |
exit(None, None, None) |
| 562 |
|
| 563 |
except that implicit :ref:`special method lookup <special-lookup>` is used |
| 564 |
for :meth:`~object.__enter__` and :meth:`~object.__exit__`. |
| 565 |
|
| 566 |
With more than one item, the context managers are processed as if multiple |
| 567 |
:keyword:`with` statements were nested:: |
| 568 |
|
| 569 |
with A() as a, B() as b: |
| 570 |
SUITE |
| 571 |
|
| 572 |
is semantically equivalent to:: |
| 573 |
|
| 574 |
with A() as a: |
| 575 |
with B() as b: |
| 576 |
SUITE |
| 577 |
|
| 578 |
You can also write multi-item context managers in multiple lines if |
| 579 |
the items are surrounded by parentheses. For example:: |
| 580 |
|
| 581 |
with ( |
| 582 |
A() as a, |
| 583 |
B() as b, |
| 584 |
): |
| 585 |
SUITE |
| 586 |
|
| 587 |
.. versionchanged:: 3.1 |
| 588 |
Support for multiple context expressions. |
| 589 |
|
| 590 |
.. versionchanged:: 3.10 |
| 591 |
Support for using grouping parentheses to break the statement in multiple lines. |
| 592 |
|
| 593 |
.. seealso:: |
| 594 |
|
| 595 |
:pep:`343` - The "with" statement |
| 596 |
The specification, background, and examples for the Python :keyword:`with` |
| 597 |
statement. |
| 598 |
|
| 599 |
.. _match: |
| 600 |
.. _case: |
| 601 |
|
| 602 |
The :keyword:`!match` statement |
| 603 |
=============================== |
| 604 |
|
| 605 |
.. index:: |
| 606 |
! pair: statement; match |
| 607 |
! pair: keyword; case |
| 608 |
! single: pattern matching |
| 609 |
pair: keyword; if |
| 610 |
pair: keyword; as |
| 611 |
pair: match; case |
| 612 |
single: as; match statement |
| 613 |
single: : (colon); compound statement |
| 614 |
|
| 615 |
.. versionadded:: 3.10 |
| 616 |
|
| 617 |
The match statement is used for pattern matching. Syntax: |
| 618 |
|
| 619 |
.. productionlist:: python-grammar |
| 620 |
match_stmt: 'match' `subject_expr` ":" NEWLINE INDENT `case_block`+ DEDENT |
| 621 |
subject_expr: `flexible_expression` "," [`flexible_expression_list` [',']] |
| 622 |
: | `assignment_expression` |
| 623 |
case_block: 'case' `patterns` [`guard`] ":" `suite` |
| 624 |
|
| 625 |
.. note:: |
| 626 |
This section uses single quotes to denote |
| 627 |
:ref:`soft keywords <soft-keywords>`. |
| 628 |
|
| 629 |
Pattern matching takes a pattern as input (following ``case``) and a subject |
| 630 |
value (following ``match``). The pattern (which may contain subpatterns) is |
| 631 |
matched against the subject value. The outcomes are: |
| 632 |
|
| 633 |
* A match success or failure (also termed a pattern success or failure). |
| 634 |
|
| 635 |
* Possible binding of matched values to a name. The prerequisites for this are |
| 636 |
further discussed below. |
| 637 |
|
| 638 |
The ``match`` and ``case`` keywords are :ref:`soft keywords <soft-keywords>`. |
| 639 |
|
| 640 |
.. seealso:: |
| 641 |
|
| 642 |
* :pep:`634` -- Structural Pattern Matching: Specification |
| 643 |
* :pep:`636` -- Structural Pattern Matching: Tutorial |
| 644 |
|
| 645 |
|
| 646 |
Overview |
| 647 |
-------- |
| 648 |
|
| 649 |
Here's an overview of the logical flow of a match statement: |
| 650 |
|
| 651 |
|
| 652 |
#. The subject expression ``subject_expr`` is evaluated and a resulting subject |
| 653 |
value obtained. If the subject expression contains a comma, a tuple is |
| 654 |
constructed using :ref:`the standard rules <typesseq-tuple>`. |
| 655 |
|
| 656 |
#. Each pattern in a ``case_block`` is attempted to match with the subject value. The |
| 657 |
specific rules for success or failure are described below. The match attempt can also |
| 658 |
bind some or all of the standalone names within the pattern. The precise |
| 659 |
pattern binding rules vary per pattern type and are |
| 660 |
specified below. **Name bindings made during a successful pattern match |
| 661 |
outlive the executed block and can be used after the match statement**. |
| 662 |
|
| 663 |
.. note:: |
| 664 |
|
| 665 |
During failed pattern matches, some subpatterns may succeed. Do not |
| 666 |
rely on bindings being made for a failed match. Conversely, do not |
| 667 |
rely on variables remaining unchanged after a failed match. The exact |
| 668 |
behavior is dependent on implementation and may vary. This is an |
| 669 |
intentional decision made to allow different implementations to add |
| 670 |
optimizations. |
| 671 |
|
| 672 |
#. If the pattern succeeds, the corresponding guard (if present) is evaluated. In |
| 673 |
this case all name bindings are guaranteed to have happened. |
| 674 |
|
| 675 |
* If the guard evaluates as true or is missing, the ``block`` inside |
| 676 |
``case_block`` is executed. |
| 677 |
|
| 678 |
* Otherwise, the next ``case_block`` is attempted as described above. |
| 679 |
|
| 680 |
* If there are no further case blocks, the match statement is completed. |
| 681 |
|
| 682 |
.. note:: |
| 683 |
|
| 684 |
Users should generally never rely on a pattern being evaluated. Depending on |
| 685 |
implementation, the interpreter may cache values or use other optimizations |
| 686 |
which skip repeated evaluations. |
| 687 |
|
| 688 |
A sample match statement:: |
| 689 |
|
| 690 |
>>> flag = False |
| 691 |
>>> match (100, 200): |
| 692 |
... case (100, 300): # Mismatch: 200 != 300 |
| 693 |
... print('Case 1') |
| 694 |
... case (100, 200) if flag: # Successful match, but guard fails |
| 695 |
... print('Case 2') |
| 696 |
... case (100, y): # Matches and binds y to 200 |
| 697 |
... print(f'Case 3, y: {y}') |
| 698 |
... case _: # Pattern not attempted |
| 699 |
... print('Case 4, I match anything!') |
| 700 |
... |
| 701 |
Case 3, y: 200 |
| 702 |
|
| 703 |
|
| 704 |
In this case, ``if flag`` is a guard. Read more about that in the next section. |
| 705 |
|
| 706 |
Guards |
| 707 |
------ |
| 708 |
|
| 709 |
.. index:: ! guard |
| 710 |
|
| 711 |
.. productionlist:: python-grammar |
| 712 |
guard: "if" `assignment_expression` |
| 713 |
|
| 714 |
A ``guard`` (which is part of the ``case``) must succeed for code inside |
| 715 |
the ``case`` block to execute. It takes the form: :keyword:`if` followed by an |
| 716 |
expression. |
| 717 |
|
| 718 |
|
| 719 |
The logical flow of a ``case`` block with a ``guard`` follows: |
| 720 |
|
| 721 |
#. Check that the pattern in the ``case`` block succeeded. If the pattern |
| 722 |
failed, the ``guard`` is not evaluated and the next ``case`` block is |
| 723 |
checked. |
| 724 |
|
| 725 |
#. If the pattern succeeded, evaluate the ``guard``. |
| 726 |
|
| 727 |
* If the ``guard`` condition evaluates as true, the case block is |
| 728 |
selected. |
| 729 |
|
| 730 |
* If the ``guard`` condition evaluates as false, the case block is not |
| 731 |
selected. |
| 732 |
|
| 733 |
* If the ``guard`` raises an exception during evaluation, the exception |
| 734 |
bubbles up. |
| 735 |
|
| 736 |
Guards are allowed to have side effects as they are expressions. Guard |
| 737 |
evaluation must proceed from the first to the last case block, one at a time, |
| 738 |
skipping case blocks whose pattern(s) don't all succeed. (I.e., |
| 739 |
guard evaluation must happen in order.) Guard evaluation must stop once a case |
| 740 |
block is selected. |
| 741 |
|
| 742 |
|
| 743 |
.. _irrefutable_case: |
| 744 |
|
| 745 |
Irrefutable Case Blocks |
| 746 |
----------------------- |
| 747 |
|
| 748 |
.. index:: irrefutable case block, case block |
| 749 |
|
| 750 |
An irrefutable case block is a match-all case block. A match statement may have |
| 751 |
at most one irrefutable case block, and it must be last. |
| 752 |
|
| 753 |
A case block is considered irrefutable if it has no guard and its pattern is |
| 754 |
irrefutable. A pattern is considered irrefutable if we can prove from its |
| 755 |
syntax alone that it will always succeed. Only the following patterns are |
| 756 |
irrefutable: |
| 757 |
|
| 758 |
* :ref:`as-patterns` whose left-hand side is irrefutable |
| 759 |
|
| 760 |
* :ref:`or-patterns` containing at least one irrefutable pattern |
| 761 |
|
| 762 |
* :ref:`capture-patterns` |
| 763 |
|
| 764 |
* :ref:`wildcard-patterns` |
| 765 |
|
| 766 |
* parenthesized irrefutable patterns |
| 767 |
|
| 768 |
|
| 769 |
Patterns |
| 770 |
-------- |
| 771 |
|
| 772 |
.. index:: |
| 773 |
single: ! patterns |
| 774 |
single: AS pattern, OR pattern, capture pattern, wildcard pattern |
| 775 |
|
| 776 |
.. note:: |
| 777 |
This section uses grammar notations beyond standard EBNF: |
| 778 |
|
| 779 |
* the notation ``SEP.RULE+`` is shorthand for ``RULE (SEP RULE)*`` |
| 780 |
|
| 781 |
* the notation ``!RULE`` is shorthand for a negative lookahead assertion |
| 782 |
|
| 783 |
|
| 784 |
The top-level syntax for ``patterns`` is: |
| 785 |
|
| 786 |
.. productionlist:: python-grammar |
| 787 |
patterns: `open_sequence_pattern` | `pattern` |
| 788 |
pattern: `as_pattern` | `or_pattern` |
| 789 |
closed_pattern: | `literal_pattern` |
| 790 |
: | `capture_pattern` |
| 791 |
: | `wildcard_pattern` |
| 792 |
: | `value_pattern` |
| 793 |
: | `group_pattern` |
| 794 |
: | `sequence_pattern` |
| 795 |
: | `mapping_pattern` |
| 796 |
: | `class_pattern` |
| 797 |
|
| 798 |
The descriptions below will include a description "in simple terms" of what a pattern |
| 799 |
does for illustration purposes (credits to Raymond Hettinger for a document that |
| 800 |
inspired most of the descriptions). Note that these descriptions are purely for |
| 801 |
illustration purposes and **may not** reflect |
| 802 |
the underlying implementation. Furthermore, they do not cover all valid forms. |
| 803 |
|
| 804 |
|
| 805 |
.. _or-patterns: |
| 806 |
|
| 807 |
OR Patterns |
| 808 |
^^^^^^^^^^^ |
| 809 |
|
| 810 |
An OR pattern is two or more patterns separated by vertical |
| 811 |
bars ``|``. Syntax: |
| 812 |
|
| 813 |
.. productionlist:: python-grammar |
| 814 |
or_pattern: "|".`closed_pattern`+ |
| 815 |
|
| 816 |
Only the final subpattern may be :ref:`irrefutable <irrefutable_case>`, and each |
| 817 |
subpattern must bind the same set of names to avoid ambiguity. |
| 818 |
|
| 819 |
An OR pattern matches each of its subpatterns in turn to the subject value, |
| 820 |
until one succeeds. The OR pattern is then considered successful. Otherwise, |
| 821 |
if none of the subpatterns succeed, the OR pattern fails. |
| 822 |
|
| 823 |
In simple terms, ``P1 | P2 | ...`` will try to match ``P1``, if it fails it will try to |
| 824 |
match ``P2``, succeeding immediately if any succeeds, failing otherwise. |
| 825 |
|
| 826 |
.. _as-patterns: |
| 827 |
|
| 828 |
AS Patterns |
| 829 |
^^^^^^^^^^^ |
| 830 |
|
| 831 |
An AS pattern matches an OR pattern on the left of the :keyword:`as` |
| 832 |
keyword against a subject. Syntax: |
| 833 |
|
| 834 |
.. productionlist:: python-grammar |
| 835 |
as_pattern: `or_pattern` "as" `capture_pattern` |
| 836 |
|
| 837 |
If the OR pattern fails, the AS pattern fails. Otherwise, the AS pattern binds |
| 838 |
the subject to the name on the right of the as keyword and succeeds. |
| 839 |
``capture_pattern`` cannot be a ``_``. |
| 840 |
|
| 841 |
In simple terms ``P as NAME`` will match with ``P``, and on success it will |
| 842 |
set ``NAME = <subject>``. |
| 843 |
|
| 844 |
|
| 845 |
.. _literal-patterns: |
| 846 |
|
| 847 |
Literal Patterns |
| 848 |
^^^^^^^^^^^^^^^^ |
| 849 |
|
| 850 |
A literal pattern corresponds to most |
| 851 |
:ref:`literals <literals>` in Python. Syntax: |
| 852 |
|
| 853 |
.. productionlist:: python-grammar |
| 854 |
literal_pattern: `signed_number` |
| 855 |
: | `signed_number` "+" NUMBER |
| 856 |
: | `signed_number` "-" NUMBER |
| 857 |
: | `strings` |
| 858 |
: | "None" |
| 859 |
: | "True" |
| 860 |
: | "False" |
| 861 |
signed_number: ["-"] NUMBER |
| 862 |
|
| 863 |
The rule ``strings`` and the token ``NUMBER`` are defined in the |
| 864 |
:doc:`standard Python grammar <./grammar>`. Triple-quoted strings are |
| 865 |
supported. Raw strings and byte strings are supported. :ref:`f-strings` |
| 866 |
and :ref:`t-strings` are not supported. |
| 867 |
|
| 868 |
The forms ``signed_number '+' NUMBER`` and ``signed_number '-' NUMBER`` are |
| 869 |
for expressing :ref:`complex numbers <imaginary>`; they require a real number |
| 870 |
on the left and an imaginary number on the right. E.g. ``3 + 4j``. |
| 871 |
|
| 872 |
In simple terms, ``LITERAL`` will succeed only if ``<subject> == LITERAL``. For |
| 873 |
the singletons ``None``, ``True`` and ``False``, the :keyword:`is` operator is used. |
| 874 |
|
| 875 |
.. _capture-patterns: |
| 876 |
|
| 877 |
Capture Patterns |
| 878 |
^^^^^^^^^^^^^^^^ |
| 879 |
|
| 880 |
A capture pattern binds the subject value to a name. |
| 881 |
Syntax: |
| 882 |
|
| 883 |
.. productionlist:: python-grammar |
| 884 |
capture_pattern: !'_' NAME |
| 885 |
|
| 886 |
A single underscore ``_`` is not a capture pattern (this is what ``!'_'`` |
| 887 |
expresses). It is instead treated as a |
| 888 |
:token:`~python-grammar:wildcard_pattern`. |
| 889 |
|
| 890 |
In a given pattern, a given name can only be bound once. E.g. |
| 891 |
``case x, x: ...`` is invalid while ``case [x] | x: ...`` is allowed. |
| 892 |
|
| 893 |
Capture patterns always succeed. The binding follows scoping rules |
| 894 |
established by the assignment expression operator in :pep:`572`; the |
| 895 |
name becomes a local variable in the closest containing function scope unless |
| 896 |
there's an applicable :keyword:`global` or :keyword:`nonlocal` statement. |
| 897 |
|
| 898 |
In simple terms ``NAME`` will always succeed and it will set ``NAME = <subject>``. |
| 899 |
|
| 900 |
.. _wildcard-patterns: |
| 901 |
|
| 902 |
Wildcard Patterns |
| 903 |
^^^^^^^^^^^^^^^^^ |
| 904 |
|
| 905 |
A wildcard pattern always succeeds (matches anything) |
| 906 |
and binds no name. Syntax: |
| 907 |
|
| 908 |
.. productionlist:: python-grammar |
| 909 |
wildcard_pattern: '_' |
| 910 |
|
| 911 |
``_`` is a :ref:`soft keyword <soft-keywords>` within any pattern, |
| 912 |
but only within patterns. It is an identifier, as usual, even within |
| 913 |
``match`` subject expressions, ``guard``\ s, and ``case`` blocks. |
| 914 |
|
| 915 |
In simple terms, ``_`` will always succeed. |
| 916 |
|
| 917 |
.. _value-patterns: |
| 918 |
|
| 919 |
Value Patterns |
| 920 |
^^^^^^^^^^^^^^ |
| 921 |
|
| 922 |
A value pattern represents a named value in Python. |
| 923 |
Syntax: |
| 924 |
|
| 925 |
.. productionlist:: python-grammar |
| 926 |
value_pattern: `attr` |
| 927 |
attr: `name_or_attr` "." NAME |
| 928 |
name_or_attr: `attr` | NAME |
| 929 |
|
| 930 |
The dotted name in the pattern is looked up using standard Python |
| 931 |
:ref:`name resolution rules <resolve_names>`. The pattern succeeds if the |
| 932 |
value found compares equal to the subject value (using the ``==`` equality |
| 933 |
operator). |
| 934 |
|
| 935 |
In simple terms ``NAME1.NAME2`` will succeed only if ``<subject> == NAME1.NAME2`` |
| 936 |
|
| 937 |
.. note:: |
| 938 |
|
| 939 |
If the same value occurs multiple times in the same match statement, the |
| 940 |
interpreter may cache the first value found and reuse it rather than repeat |
| 941 |
the same lookup. This cache is strictly tied to a given execution of a |
| 942 |
given match statement. |
| 943 |
|
| 944 |
.. _group-patterns: |
| 945 |
|
| 946 |
Group Patterns |
| 947 |
^^^^^^^^^^^^^^ |
| 948 |
|
| 949 |
A group pattern allows users to add parentheses around patterns to |
| 950 |
emphasize the intended grouping. Otherwise, it has no additional syntax. |
| 951 |
Syntax: |
| 952 |
|
| 953 |
.. productionlist:: python-grammar |
| 954 |
group_pattern: "(" `pattern` ")" |
| 955 |
|
| 956 |
In simple terms ``(P)`` has the same effect as ``P``. |
| 957 |
|
| 958 |
.. _sequence-patterns: |
| 959 |
|
| 960 |
Sequence Patterns |
| 961 |
^^^^^^^^^^^^^^^^^ |
| 962 |
|
| 963 |
A sequence pattern contains several subpatterns to be matched against sequence elements. |
| 964 |
The syntax is similar to the unpacking of a list or tuple. |
| 965 |
|
| 966 |
.. productionlist:: python-grammar |
| 967 |
sequence_pattern: "[" [`maybe_sequence_pattern`] "]" |
| 968 |
: | "(" [`open_sequence_pattern`] ")" |
| 969 |
open_sequence_pattern: `maybe_star_pattern` "," [`maybe_sequence_pattern`] |
| 970 |
maybe_sequence_pattern: ",".`maybe_star_pattern`+ ","? |
| 971 |
maybe_star_pattern: `star_pattern` | `pattern` |
| 972 |
star_pattern: "*" (`capture_pattern` | `wildcard_pattern`) |
| 973 |
|
| 974 |
There is no difference if parentheses or square brackets |
| 975 |
are used for sequence patterns (i.e. ``(...)`` vs ``[...]`` ). |
| 976 |
|
| 977 |
.. note:: |
| 978 |
A single pattern enclosed in parentheses without a trailing comma |
| 979 |
(e.g. ``(3 | 4)``) is a :ref:`group pattern <group-patterns>`. |
| 980 |
While a single pattern enclosed in square brackets (e.g. ``[3 | 4]``) is |
| 981 |
still a sequence pattern. |
| 982 |
|
| 983 |
At most one star subpattern may be in a sequence pattern. The star subpattern |
| 984 |
may occur in any position. If no star subpattern is present, the sequence |
| 985 |
pattern is a fixed-length sequence pattern; otherwise it is a variable-length |
| 986 |
sequence pattern. |
| 987 |
|
| 988 |
The following is the logical flow for matching a sequence pattern against a |
| 989 |
subject value: |
| 990 |
|
| 991 |
#. If the subject value is not a sequence [#]_, the sequence pattern |
| 992 |
fails. |
| 993 |
|
| 994 |
#. If the subject value is an instance of ``str``, ``bytes`` or ``bytearray`` |
| 995 |
the sequence pattern fails. |
| 996 |
|
| 997 |
#. The subsequent steps depend on whether the sequence pattern is fixed or |
| 998 |
variable-length. |
| 999 |
|
| 1000 |
If the sequence pattern is fixed-length: |
| 1001 |
|
| 1002 |
#. If the length of the subject sequence is not equal to the number of |
| 1003 |
subpatterns, the sequence pattern fails |
| 1004 |
|
| 1005 |
#. Subpatterns in the sequence pattern are matched to their corresponding |
| 1006 |
items in the subject sequence from left to right. Matching stops as soon |
| 1007 |
as a subpattern fails. If all subpatterns succeed in matching their |
| 1008 |
corresponding item, the sequence pattern succeeds. |
| 1009 |
|
| 1010 |
Otherwise, if the sequence pattern is variable-length: |
| 1011 |
|
| 1012 |
#. If the length of the subject sequence is less than the number of non-star |
| 1013 |
subpatterns, the sequence pattern fails. |
| 1014 |
|
| 1015 |
#. The leading non-star subpatterns are matched to their corresponding items |
| 1016 |
as for fixed-length sequences. |
| 1017 |
|
| 1018 |
#. If the previous step succeeds, the star subpattern matches a list formed |
| 1019 |
of the remaining subject items, excluding the remaining items |
| 1020 |
corresponding to non-star subpatterns following the star subpattern. |
| 1021 |
|
| 1022 |
#. Remaining non-star subpatterns are matched to their corresponding subject |
| 1023 |
items, as for a fixed-length sequence. |
| 1024 |
|
| 1025 |
.. note:: The length of the subject sequence is obtained via |
| 1026 |
:func:`len` (i.e. via the :meth:`~object.__len__` protocol). |
| 1027 |
This length may be cached by the interpreter in a similar manner as |
| 1028 |
:ref:`value patterns <value-patterns>`. |
| 1029 |
|
| 1030 |
|
| 1031 |
In simple terms ``[P1, P2, P3,`` ... ``, P<N>]`` matches only if all the following |
| 1032 |
happens: |
| 1033 |
|
| 1034 |
* check ``<subject>`` is a sequence |
| 1035 |
* ``len(subject) == <N>`` |
| 1036 |
* ``P1`` matches ``<subject>[0]`` (note that this match can also bind names) |
| 1037 |
* ``P2`` matches ``<subject>[1]`` (note that this match can also bind names) |
| 1038 |
* ... and so on for the corresponding pattern/element. |
| 1039 |
|
| 1040 |
.. _mapping-patterns: |
| 1041 |
|
| 1042 |
Mapping Patterns |
| 1043 |
^^^^^^^^^^^^^^^^ |
| 1044 |
|
| 1045 |
A mapping pattern contains one or more key-value patterns. The syntax is |
| 1046 |
similar to the construction of a dictionary. |
| 1047 |
Syntax: |
| 1048 |
|
| 1049 |
.. productionlist:: python-grammar |
| 1050 |
mapping_pattern: "{" [`items_pattern`] "}" |
| 1051 |
items_pattern: ",".`key_value_pattern`+ ","? |
| 1052 |
key_value_pattern: (`literal_pattern` | `value_pattern`) ":" `pattern` |
| 1053 |
: | `double_star_pattern` |
| 1054 |
double_star_pattern: "**" `capture_pattern` |
| 1055 |
|
| 1056 |
At most one double star pattern may be in a mapping pattern. The double star |
| 1057 |
pattern must be the last subpattern in the mapping pattern. |
| 1058 |
|
| 1059 |
Duplicate keys in mapping patterns are disallowed. Duplicate literal keys will |
| 1060 |
raise a :exc:`SyntaxError`. Two keys that otherwise have the same value will |
| 1061 |
raise a :exc:`ValueError` at runtime. |
| 1062 |
|
| 1063 |
The following is the logical flow for matching a mapping pattern against a |
| 1064 |
subject value: |
| 1065 |
|
| 1066 |
#. If the subject value is not a mapping [#]_,the mapping pattern fails. |
| 1067 |
|
| 1068 |
#. If every key given in the mapping pattern is present in the subject mapping, |
| 1069 |
and the pattern for each key matches the corresponding item of the subject |
| 1070 |
mapping, the mapping pattern succeeds. |
| 1071 |
|
| 1072 |
#. If duplicate keys are detected in the mapping pattern, the pattern is |
| 1073 |
considered invalid. A :exc:`SyntaxError` is raised for duplicate literal |
| 1074 |
values; or a :exc:`ValueError` for named keys of the same value. |
| 1075 |
|
| 1076 |
.. note:: Key-value pairs are matched using the two-argument form of the mapping |
| 1077 |
subject's ``get()`` method. Matched key-value pairs must already be present |
| 1078 |
in the mapping, and not created on-the-fly via :meth:`~object.__missing__` |
| 1079 |
or :meth:`~object.__getitem__`. |
| 1080 |
|
| 1081 |
In simple terms ``{KEY1: P1, KEY2: P2, ... }`` matches only if all the following |
| 1082 |
happens: |
| 1083 |
|
| 1084 |
* check ``<subject>`` is a mapping |
| 1085 |
* ``KEY1 in <subject>`` |
| 1086 |
* ``P1`` matches ``<subject>[KEY1]`` |
| 1087 |
* ... and so on for the corresponding KEY/pattern pair. |
| 1088 |
|
| 1089 |
|
| 1090 |
.. _class-patterns: |
| 1091 |
|
| 1092 |
Class Patterns |
| 1093 |
^^^^^^^^^^^^^^ |
| 1094 |
|
| 1095 |
A class pattern represents a class and its positional and keyword arguments |
| 1096 |
(if any). Syntax: |
| 1097 |
|
| 1098 |
.. productionlist:: python-grammar |
| 1099 |
class_pattern: `name_or_attr` "(" [`pattern_arguments` ","?] ")" |
| 1100 |
pattern_arguments: `positional_patterns` ["," `keyword_patterns`] |
| 1101 |
: | `keyword_patterns` |
| 1102 |
positional_patterns: ",".`pattern`+ |
| 1103 |
keyword_patterns: ",".`keyword_pattern`+ |
| 1104 |
keyword_pattern: NAME "=" `pattern` |
| 1105 |
|
| 1106 |
The same keyword should not be repeated in class patterns. |
| 1107 |
|
| 1108 |
The following is the logical flow for matching a class pattern against a |
| 1109 |
subject value: |
| 1110 |
|
| 1111 |
#. If ``name_or_attr`` is not an instance of the builtin :class:`type` , raise |
| 1112 |
:exc:`TypeError`. |
| 1113 |
|
| 1114 |
#. If the subject value is not an instance of ``name_or_attr`` (tested via |
| 1115 |
:func:`isinstance`), the class pattern fails. |
| 1116 |
|
| 1117 |
#. If no pattern arguments are present, the pattern succeeds. Otherwise, |
| 1118 |
the subsequent steps depend on whether keyword or positional argument patterns |
| 1119 |
are present. |
| 1120 |
|
| 1121 |
For a number of built-in types (specified below), a single positional |
| 1122 |
subpattern is accepted which will match the entire subject; for these types |
| 1123 |
keyword patterns also work as for other types. |
| 1124 |
|
| 1125 |
If only keyword patterns are present, they are processed as follows, |
| 1126 |
one by one: |
| 1127 |
|
| 1128 |
I. The keyword is looked up as an attribute on the subject. |
| 1129 |
|
| 1130 |
* If this raises an exception other than :exc:`AttributeError`, the |
| 1131 |
exception bubbles up. |
| 1132 |
|
| 1133 |
* If this raises :exc:`AttributeError`, the class pattern has failed. |
| 1134 |
|
| 1135 |
* Else, the subpattern associated with the keyword pattern is matched |
| 1136 |
against the subject's attribute value. If this fails, the class |
| 1137 |
pattern fails; if this succeeds, the match proceeds to the next keyword. |
| 1138 |
|
| 1139 |
|
| 1140 |
II. If all keyword patterns succeed, the class pattern succeeds. |
| 1141 |
|
| 1142 |
If any positional patterns are present, they are converted to keyword |
| 1143 |
patterns using the :data:`~object.__match_args__` attribute on the class |
| 1144 |
``name_or_attr`` before matching: |
| 1145 |
|
| 1146 |
I. The equivalent of ``getattr(cls, "__match_args__", ())`` is called. |
| 1147 |
|
| 1148 |
* If this raises an exception, the exception bubbles up. |
| 1149 |
|
| 1150 |
* If the returned value is not a tuple, the conversion fails and |
| 1151 |
:exc:`TypeError` is raised. |
| 1152 |
|
| 1153 |
* If there are more positional patterns than ``len(cls.__match_args__)``, |
| 1154 |
:exc:`TypeError` is raised. |
| 1155 |
|
| 1156 |
* Otherwise, positional pattern ``i`` is converted to a keyword pattern |
| 1157 |
using ``__match_args__[i]`` as the keyword. ``__match_args__[i]`` must |
| 1158 |
be a string; if not :exc:`TypeError` is raised. |
| 1159 |
|
| 1160 |
* If there are duplicate keywords, :exc:`TypeError` is raised. |
| 1161 |
|
| 1162 |
.. seealso:: :ref:`class-pattern-matching` |
| 1163 |
|
| 1164 |
II. Once all positional patterns have been converted to keyword patterns, |
| 1165 |
the match proceeds as if there were only keyword patterns. |
| 1166 |
|
| 1167 |
For the following built-in types the handling of positional subpatterns is |
| 1168 |
different: |
| 1169 |
|
| 1170 |
* :class:`bool` |
| 1171 |
* :class:`bytearray` |
| 1172 |
* :class:`bytes` |
| 1173 |
* :class:`dict` |
| 1174 |
* :class:`float` |
| 1175 |
* :class:`frozenset` |
| 1176 |
* :class:`int` |
| 1177 |
* :class:`list` |
| 1178 |
* :class:`set` |
| 1179 |
* :class:`str` |
| 1180 |
* :class:`tuple` |
| 1181 |
|
| 1182 |
These classes accept a single positional argument, and the pattern there is matched |
| 1183 |
against the whole object rather than an attribute. For example ``int(0|1)`` matches |
| 1184 |
the value ``0``, but not the value ``0.0``. |
| 1185 |
|
| 1186 |
In simple terms ``CLS(P1, attr=P2)`` matches only if the following happens: |
| 1187 |
|
| 1188 |
* ``isinstance(<subject>, CLS)`` |
| 1189 |
* convert ``P1`` to a keyword pattern using ``CLS.__match_args__`` |
| 1190 |
* For each keyword argument ``attr=P2``: |
| 1191 |
|
| 1192 |
* ``hasattr(<subject>, "attr")`` |
| 1193 |
* ``P2`` matches ``<subject>.attr`` |
| 1194 |
|
| 1195 |
* ... and so on for the corresponding keyword argument/pattern pair. |
| 1196 |
|
| 1197 |
.. seealso:: |
| 1198 |
|
| 1199 |
* :pep:`634` -- Structural Pattern Matching: Specification |
| 1200 |
* :pep:`636` -- Structural Pattern Matching: Tutorial |
| 1201 |
|
| 1202 |
|
| 1203 |
.. index:: |
| 1204 |
single: parameter; function definition |
| 1205 |
|
| 1206 |
.. _function: |
| 1207 |
.. _def: |
| 1208 |
|
| 1209 |
Function definitions |
| 1210 |
==================== |
| 1211 |
|
| 1212 |
.. index:: |
| 1213 |
pair: statement; def |
| 1214 |
pair: function; definition |
| 1215 |
pair: function; name |
| 1216 |
pair: name; binding |
| 1217 |
pair: object; user-defined function |
| 1218 |
pair: object; function |
| 1219 |
pair: function; name |
| 1220 |
pair: name; binding |
| 1221 |
single: () (parentheses); function definition |
| 1222 |
single: , (comma); parameter list |
| 1223 |
single: : (colon); compound statement |
| 1224 |
|
| 1225 |
A function definition defines a user-defined function object (see section |
| 1226 |
:ref:`types`): |
| 1227 |
|
| 1228 |
.. productionlist:: python-grammar |
| 1229 |
funcdef: [`decorators`] "def" `funcname` [`type_params`] "(" [`parameter_list`] ")" |
| 1230 |
: ["->" `expression`] ":" `suite` |
| 1231 |
decorators: `decorator`+ |
| 1232 |
decorator: "@" `assignment_expression` NEWLINE |
| 1233 |
parameter_list: `defparameter` ("," `defparameter`)* "," "/" ["," [`parameter_list_no_posonly`]] |
| 1234 |
: | `parameter_list_no_posonly` |
| 1235 |
parameter_list_no_posonly: `defparameter` ("," `defparameter`)* ["," [`parameter_list_starargs`]] |
| 1236 |
: | `parameter_list_starargs` |
| 1237 |
parameter_list_starargs: "*" [`star_parameter`] ("," `defparameter`)* ["," [`parameter_star_kwargs`]] |
| 1238 |
: | "*" ("," `defparameter`)+ ["," [`parameter_star_kwargs`]] |
| 1239 |
: | `parameter_star_kwargs` |
| 1240 |
parameter_star_kwargs: "**" `parameter` [","] |
| 1241 |
parameter: `identifier` [":" `expression`] |
| 1242 |
star_parameter: `identifier` [":" ["*"] `expression`] |
| 1243 |
defparameter: `parameter` ["=" `expression`] |
| 1244 |
funcname: `identifier` |
| 1245 |
|
| 1246 |
|
| 1247 |
A function definition is an executable statement. Its execution binds the |
| 1248 |
function name in the current local namespace to a function object (a wrapper |
| 1249 |
around the executable code for the function). This function object contains a |
| 1250 |
reference to the current global namespace as the global namespace to be used |
| 1251 |
when the function is called. |
| 1252 |
|
| 1253 |
The function definition does not execute the function body; this gets executed |
| 1254 |
only when the function is called. [#]_ |
| 1255 |
|
| 1256 |
.. index:: |
| 1257 |
single: @ (at); function definition |
| 1258 |
|
| 1259 |
A function definition may be wrapped by one or more :term:`decorator` expressions. |
| 1260 |
Decorator expressions are evaluated when the function is defined, in the scope |
| 1261 |
that contains the function definition. The result must be a callable, which is |
| 1262 |
invoked with the function object as the only argument. The returned value is |
| 1263 |
bound to the function name instead of the function object. Multiple decorators |
| 1264 |
are applied in nested fashion. For example, the following code :: |
| 1265 |
|
| 1266 |
@f1(arg) |
| 1267 |
@f2 |
| 1268 |
def func(): pass |
| 1269 |
|
| 1270 |
is roughly equivalent to :: |
| 1271 |
|
| 1272 |
def func(): pass |
| 1273 |
func = f1(arg)(f2(func)) |
| 1274 |
|
| 1275 |
except that the original function is not temporarily bound to the name ``func``. |
| 1276 |
|
| 1277 |
.. versionchanged:: 3.9 |
| 1278 |
Functions may be decorated with any valid |
| 1279 |
:token:`~python-grammar:assignment_expression`. Previously, the grammar was |
| 1280 |
much more restrictive; see :pep:`614` for details. |
| 1281 |
|
| 1282 |
A list of :ref:`type parameters <type-params>` may be given in square brackets |
| 1283 |
between the function's name and the opening parenthesis for its parameter list. |
| 1284 |
This indicates to static type checkers that the function is generic. At runtime, |
| 1285 |
the type parameters can be retrieved from the function's |
| 1286 |
:attr:`~function.__type_params__` |
| 1287 |
attribute. See :ref:`generic-functions` for more. |
| 1288 |
|
| 1289 |
.. versionchanged:: 3.12 |
| 1290 |
Type parameter lists are new in Python 3.12. |
| 1291 |
|
| 1292 |
.. index:: |
| 1293 |
triple: default; parameter; value |
| 1294 |
single: argument; function definition |
| 1295 |
single: = (equals); function definition |
| 1296 |
|
| 1297 |
When one or more :term:`parameters <parameter>` have the form *parameter* ``=`` |
| 1298 |
*expression*, the function is said to have "default parameter values." For a |
| 1299 |
parameter with a default value, the corresponding :term:`argument` may be |
| 1300 |
omitted from a call, in which |
| 1301 |
case the parameter's default value is substituted. If a parameter has a default |
| 1302 |
value, all following parameters up until the "``*``" must also have a default |
| 1303 |
value --- this is a syntactic restriction that is not expressed by the grammar. |
| 1304 |
|
| 1305 |
**Default parameter values are evaluated from left to right when the function |
| 1306 |
definition is executed.** This means that the expression is evaluated once, when |
| 1307 |
the function is defined, and that the same "pre-computed" value is used for each |
| 1308 |
call. This is especially important to understand when a default parameter value is a |
| 1309 |
mutable object, such as a list or a dictionary: if the function modifies the |
| 1310 |
object (e.g. by appending an item to a list), the default parameter value is in effect |
| 1311 |
modified. This is generally not what was intended. A way around this is to use |
| 1312 |
``None`` as the default, and explicitly test for it in the body of the function, |
| 1313 |
e.g.:: |
| 1314 |
|
| 1315 |
def whats_on_the_telly(penguin=None): |
| 1316 |
if penguin is None: |
| 1317 |
penguin = [] |
| 1318 |
penguin.append("property of the zoo") |
| 1319 |
return penguin |
| 1320 |
|
| 1321 |
.. index:: |
| 1322 |
single: / (slash); function definition |
| 1323 |
single: * (asterisk); function definition |
| 1324 |
single: **; function definition |
| 1325 |
|
| 1326 |
Function call semantics are described in more detail in section :ref:`calls`. A |
| 1327 |
function call always assigns values to all parameters mentioned in the parameter |
| 1328 |
list, either from positional arguments, from keyword arguments, or from default |
| 1329 |
values. If the form "``*identifier``" is present, it is initialized to a tuple |
| 1330 |
receiving any excess positional parameters, defaulting to the empty tuple. |
| 1331 |
If the form "``**identifier``" is present, it is initialized to a new |
| 1332 |
ordered mapping receiving any excess keyword arguments, defaulting to a |
| 1333 |
new empty mapping of the same type. Parameters after "``*``" or |
| 1334 |
"``*identifier``" are keyword-only parameters and may only be passed |
| 1335 |
by keyword arguments. Parameters before "``/``" are positional-only parameters |
| 1336 |
and may only be passed by positional arguments. |
| 1337 |
|
| 1338 |
.. versionchanged:: 3.8 |
| 1339 |
The ``/`` function parameter syntax may be used to indicate positional-only |
| 1340 |
parameters. See :pep:`570` for details. |
| 1341 |
|
| 1342 |
.. index:: |
| 1343 |
pair: function; annotations |
| 1344 |
single: ->; function annotations |
| 1345 |
single: : (colon); function annotations |
| 1346 |
|
| 1347 |
Parameters may have an :term:`annotation <function annotation>` of the form "``: expression``" |
| 1348 |
following the parameter name. Any parameter may have an annotation, even those of the form |
| 1349 |
``*identifier`` or ``**identifier``. (As a special case, parameters of the form |
| 1350 |
``*identifier`` may have an annotation "``: *expression``".) Functions may have "return" annotation of |
| 1351 |
the form "``-> expression``" after the parameter list. These annotations can be |
| 1352 |
any valid Python expression. The presence of annotations does not change the |
| 1353 |
semantics of a function. See :ref:`annotations` for more information on annotations. |
| 1354 |
|
| 1355 |
.. versionchanged:: 3.11 |
| 1356 |
Parameters of the form "``*identifier``" may have an annotation |
| 1357 |
"``: *expression``". See :pep:`646`. |
| 1358 |
|
| 1359 |
.. index:: pair: lambda; expression |
| 1360 |
|
| 1361 |
It is also possible to create anonymous functions (functions not bound to a |
| 1362 |
name), for immediate use in expressions. This uses lambda expressions, described in |
| 1363 |
section :ref:`lambda`. Note that the lambda expression is merely a shorthand for a |
| 1364 |
simplified function definition; a function defined in a ":keyword:`def`" |
| 1365 |
statement can be passed around or assigned to another name just like a function |
| 1366 |
defined by a lambda expression. The ":keyword:`!def`" form is actually more powerful |
| 1367 |
since it allows the execution of multiple statements and annotations. |
| 1368 |
|
| 1369 |
**Programmer's note:** Functions are first-class objects. A "``def``" statement |
| 1370 |
executed inside a function definition defines a local function that can be |
| 1371 |
returned or passed around. Free variables used in the nested function can |
| 1372 |
access the local variables of the function containing the def. See section |
| 1373 |
:ref:`naming` for details. |
| 1374 |
|
| 1375 |
.. seealso:: |
| 1376 |
|
| 1377 |
:pep:`3107` - Function Annotations |
| 1378 |
The original specification for function annotations. |
| 1379 |
|
| 1380 |
:pep:`484` - Type Hints |
| 1381 |
Definition of a standard meaning for annotations: type hints. |
| 1382 |
|
| 1383 |
:pep:`526` - Syntax for Variable Annotations |
| 1384 |
Ability to type hint variable declarations, including class |
| 1385 |
variables and instance variables. |
| 1386 |
|
| 1387 |
:pep:`563` - Postponed Evaluation of Annotations |
| 1388 |
Support for forward references within annotations by preserving |
| 1389 |
annotations in a string form at runtime instead of eager evaluation. |
| 1390 |
|
| 1391 |
:pep:`318` - Decorators for Functions and Methods |
| 1392 |
Function and method decorators were introduced. |
| 1393 |
Class decorators were introduced in :pep:`3129`. |
| 1394 |
|
| 1395 |
.. _class: |
| 1396 |
|
| 1397 |
Class definitions |
| 1398 |
================= |
| 1399 |
|
| 1400 |
.. index:: |
| 1401 |
pair: object; class |
| 1402 |
pair: statement; class |
| 1403 |
pair: class; definition |
| 1404 |
pair: class; name |
| 1405 |
pair: name; binding |
| 1406 |
pair: execution; frame |
| 1407 |
single: inheritance |
| 1408 |
single: docstring |
| 1409 |
single: () (parentheses); class definition |
| 1410 |
single: , (comma); expression list |
| 1411 |
single: : (colon); compound statement |
| 1412 |
|
| 1413 |
A class definition defines a class object (see section :ref:`types`): |
| 1414 |
|
| 1415 |
.. productionlist:: python-grammar |
| 1416 |
classdef: [`decorators`] "class" `classname` [`type_params`] [`inheritance`] ":" `suite` |
| 1417 |
inheritance: "(" [`argument_list`] ")" |
| 1418 |
classname: `identifier` |
| 1419 |
|
| 1420 |
A class definition is an executable statement. The inheritance list usually |
| 1421 |
gives a list of base classes (see :ref:`metaclasses` for more advanced uses), so |
| 1422 |
each item in the list should evaluate to a class object which allows |
| 1423 |
subclassing. Classes without an inheritance list inherit, by default, from the |
| 1424 |
base class :class:`object`; hence, :: |
| 1425 |
|
| 1426 |
class Foo: |
| 1427 |
pass |
| 1428 |
|
| 1429 |
is equivalent to :: |
| 1430 |
|
| 1431 |
class Foo(object): |
| 1432 |
pass |
| 1433 |
|
| 1434 |
The class's suite is then executed in a new execution frame (see :ref:`naming`), |
| 1435 |
using a newly created local namespace and the original global namespace. |
| 1436 |
(Usually, the suite contains mostly function definitions.) When the class's |
| 1437 |
suite finishes execution, its execution frame is discarded but its local |
| 1438 |
namespace is saved. [#]_ A class object is then created using the inheritance |
| 1439 |
list for the base classes and the saved local namespace for the attribute |
| 1440 |
dictionary. The class name is bound to this class object in the original local |
| 1441 |
namespace. |
| 1442 |
|
| 1443 |
The order in which attributes are defined in the class body is preserved |
| 1444 |
in the new class's :attr:`~type.__dict__`. Note that this is reliable only right |
| 1445 |
after the class is created and only for classes that were defined using |
| 1446 |
the definition syntax. |
| 1447 |
|
| 1448 |
Class creation can be customized heavily using :ref:`metaclasses <metaclasses>`. |
| 1449 |
|
| 1450 |
.. index:: |
| 1451 |
single: @ (at); class definition |
| 1452 |
|
| 1453 |
Classes can also be decorated: just like when decorating functions, :: |
| 1454 |
|
| 1455 |
@f1(arg) |
| 1456 |
@f2 |
| 1457 |
class Foo: pass |
| 1458 |
|
| 1459 |
is roughly equivalent to :: |
| 1460 |
|
| 1461 |
class Foo: pass |
| 1462 |
Foo = f1(arg)(f2(Foo)) |
| 1463 |
|
| 1464 |
The evaluation rules for the decorator expressions are the same as for function |
| 1465 |
decorators. The result is then bound to the class name. |
| 1466 |
|
| 1467 |
.. versionchanged:: 3.9 |
| 1468 |
Classes may be decorated with any valid |
| 1469 |
:token:`~python-grammar:assignment_expression`. Previously, the grammar was |
| 1470 |
much more restrictive; see :pep:`614` for details. |
| 1471 |
|
| 1472 |
A list of :ref:`type parameters <type-params>` may be given in square brackets |
| 1473 |
immediately after the class's name. |
| 1474 |
This indicates to static type checkers that the class is generic. At runtime, |
| 1475 |
the type parameters can be retrieved from the class's |
| 1476 |
:attr:`~type.__type_params__` attribute. See :ref:`generic-classes` for more. |
| 1477 |
|
| 1478 |
.. versionchanged:: 3.12 |
| 1479 |
Type parameter lists are new in Python 3.12. |
| 1480 |
|
| 1481 |
**Programmer's note:** Variables defined in the class definition are class |
| 1482 |
attributes; they are shared by instances. Instance attributes can be set in a |
| 1483 |
method with ``self.name = value``. Both class and instance attributes are |
| 1484 |
accessible through the notation "``self.name``", and an instance attribute hides |
| 1485 |
a class attribute with the same name when accessed in this way. Class |
| 1486 |
attributes can be used as defaults for instance attributes, but using mutable |
| 1487 |
values there can lead to unexpected results. :ref:`Descriptors <descriptors>` |
| 1488 |
can be used to create instance variables with different implementation details. |
| 1489 |
|
| 1490 |
|
| 1491 |
.. seealso:: |
| 1492 |
|
| 1493 |
:pep:`3115` - Metaclasses in Python 3000 |
| 1494 |
The proposal that changed the declaration of metaclasses to the current |
| 1495 |
syntax, and the semantics for how classes with metaclasses are |
| 1496 |
constructed. |
| 1497 |
|
| 1498 |
:pep:`3129` - Class Decorators |
| 1499 |
The proposal that added class decorators. Function and method decorators |
| 1500 |
were introduced in :pep:`318`. |
| 1501 |
|
| 1502 |
|
| 1503 |
.. _async: |
| 1504 |
|
| 1505 |
Coroutines |
| 1506 |
========== |
| 1507 |
|
| 1508 |
.. versionadded:: 3.5 |
| 1509 |
|
| 1510 |
.. index:: pair: statement; async def |
| 1511 |
.. _`async def`: |
| 1512 |
|
| 1513 |
Coroutine function definition |
| 1514 |
----------------------------- |
| 1515 |
|
| 1516 |
.. productionlist:: python-grammar |
| 1517 |
async_funcdef: [`decorators`] "async" "def" `funcname` "(" [`parameter_list`] ")" |
| 1518 |
: ["->" `expression`] ":" `suite` |
| 1519 |
|
| 1520 |
.. index:: |
| 1521 |
pair: keyword; async |
| 1522 |
pair: keyword; await |
| 1523 |
|
| 1524 |
Execution of Python coroutines can be suspended and resumed at many points |
| 1525 |
(see :term:`coroutine`). :keyword:`await` expressions, :keyword:`async for` and |
| 1526 |
:keyword:`async with` can only be used in the body of a coroutine function. |
| 1527 |
|
| 1528 |
Functions defined with ``async def`` syntax are always coroutine functions, |
| 1529 |
even if they do not contain ``await`` or ``async`` keywords. |
| 1530 |
|
| 1531 |
It is a :exc:`SyntaxError` to use a ``yield from`` expression inside the body |
| 1532 |
of a coroutine function. |
| 1533 |
|
| 1534 |
An example of a coroutine function:: |
| 1535 |
|
| 1536 |
async def func(param1, param2): |
| 1537 |
do_stuff() |
| 1538 |
await some_coroutine() |
| 1539 |
|
| 1540 |
.. versionchanged:: 3.7 |
| 1541 |
``await`` and ``async`` are now keywords; previously they were only |
| 1542 |
treated as such inside the body of a coroutine function. |
| 1543 |
|
| 1544 |
.. index:: pair: statement; async for |
| 1545 |
.. _`async for`: |
| 1546 |
|
| 1547 |
The :keyword:`!async for` statement |
| 1548 |
----------------------------------- |
| 1549 |
|
| 1550 |
.. productionlist:: python-grammar |
| 1551 |
async_for_stmt: "async" `for_stmt` |
| 1552 |
|
| 1553 |
An :term:`asynchronous iterable` provides an ``__aiter__`` method that directly |
| 1554 |
returns an :term:`asynchronous iterator`, which can call asynchronous code in |
| 1555 |
its ``__anext__`` method. |
| 1556 |
|
| 1557 |
The ``async for`` statement allows convenient iteration over asynchronous |
| 1558 |
iterables. |
| 1559 |
|
| 1560 |
The following code:: |
| 1561 |
|
| 1562 |
async for TARGET in ITER: |
| 1563 |
SUITE |
| 1564 |
else: |
| 1565 |
SUITE2 |
| 1566 |
|
| 1567 |
Is semantically equivalent to:: |
| 1568 |
|
| 1569 |
iter = (ITER).__aiter__() |
| 1570 |
running = True |
| 1571 |
|
| 1572 |
while running: |
| 1573 |
try: |
| 1574 |
TARGET = await iter.__anext__() |
| 1575 |
except StopAsyncIteration: |
| 1576 |
running = False |
| 1577 |
else: |
| 1578 |
SUITE |
| 1579 |
else: |
| 1580 |
SUITE2 |
| 1581 |
|
| 1582 |
except that implicit :ref:`special method lookup <special-lookup>` is used |
| 1583 |
for :meth:`~object.__aiter__` and :meth:`~object.__anext__`. |
| 1584 |
|
| 1585 |
It is a :exc:`SyntaxError` to use an ``async for`` statement outside the |
| 1586 |
body of a coroutine function. |
| 1587 |
|
| 1588 |
|
| 1589 |
.. index:: pair: statement; async with |
| 1590 |
.. _`async with`: |
| 1591 |
|
| 1592 |
The :keyword:`!async with` statement |
| 1593 |
------------------------------------ |
| 1594 |
|
| 1595 |
.. productionlist:: python-grammar |
| 1596 |
async_with_stmt: "async" `with_stmt` |
| 1597 |
|
| 1598 |
An :term:`asynchronous context manager` is a :term:`context manager` that is |
| 1599 |
able to suspend execution in its *enter* and *exit* methods. |
| 1600 |
|
| 1601 |
The following code:: |
| 1602 |
|
| 1603 |
async with EXPRESSION as TARGET: |
| 1604 |
SUITE |
| 1605 |
|
| 1606 |
is semantically equivalent to:: |
| 1607 |
|
| 1608 |
manager = (EXPRESSION) |
| 1609 |
aenter = manager.__aenter__ |
| 1610 |
aexit = manager.__aexit__ |
| 1611 |
value = await aenter() |
| 1612 |
hit_except = False |
| 1613 |
|
| 1614 |
try: |
| 1615 |
TARGET = value |
| 1616 |
SUITE |
| 1617 |
except: |
| 1618 |
hit_except = True |
| 1619 |
if not await aexit(*sys.exc_info()): |
| 1620 |
raise |
| 1621 |
finally: |
| 1622 |
if not hit_except: |
| 1623 |
await aexit(None, None, None) |
| 1624 |
|
| 1625 |
except that implicit :ref:`special method lookup <special-lookup>` is used |
| 1626 |
for :meth:`~object.__aenter__` and :meth:`~object.__aexit__`. |
| 1627 |
|
| 1628 |
It is a :exc:`SyntaxError` to use an ``async with`` statement outside the |
| 1629 |
body of a coroutine function. |
| 1630 |
|
| 1631 |
.. seealso:: |
| 1632 |
|
| 1633 |
:pep:`492` - Coroutines with async and await syntax |
| 1634 |
The proposal that made coroutines a proper standalone concept in Python, |
| 1635 |
and added supporting syntax. |
| 1636 |
|
| 1637 |
.. _type-params: |
| 1638 |
|
| 1639 |
Type parameter lists |
| 1640 |
==================== |
| 1641 |
|
| 1642 |
.. versionadded:: 3.12 |
| 1643 |
|
| 1644 |
.. versionchanged:: 3.13 |
| 1645 |
Support for default values was added (see :pep:`696`). |
| 1646 |
|
| 1647 |
.. index:: |
| 1648 |
single: type parameters |
| 1649 |
|
| 1650 |
.. productionlist:: python-grammar |
| 1651 |
type_params: "[" `type_param` ("," `type_param`)* "]" |
| 1652 |
type_param: `typevar` | `typevartuple` | `paramspec` |
| 1653 |
typevar: `identifier` (":" `expression`)? ("=" `expression`)? |
| 1654 |
typevartuple: "*" `identifier` ("=" `expression`)? |
| 1655 |
paramspec: "**" `identifier` ("=" `expression`)? |
| 1656 |
|
| 1657 |
:ref:`Functions <def>` (including :ref:`coroutines <async def>`), |
| 1658 |
:ref:`classes <class>` and :ref:`type aliases <type>` may |
| 1659 |
contain a type parameter list:: |
| 1660 |
|
| 1661 |
def max[T](args: list[T]) -> T: |
| 1662 |
... |
| 1663 |
|
| 1664 |
async def amax[T](args: list[T]) -> T: |
| 1665 |
... |
| 1666 |
|
| 1667 |
class Bag[T]: |
| 1668 |
def __iter__(self) -> Iterator[T]: |
| 1669 |
... |
| 1670 |
|
| 1671 |
def add(self, arg: T) -> None: |
| 1672 |
... |
| 1673 |
|
| 1674 |
type ListOrSet[T] = list[T] | set[T] |
| 1675 |
|
| 1676 |
Semantically, this indicates that the function, class, or type alias is |
| 1677 |
generic over a type variable. This information is primarily used by static |
| 1678 |
type checkers, and at runtime, generic objects behave much like their |
| 1679 |
non-generic counterparts. |
| 1680 |
|
| 1681 |
Type parameters are declared in square brackets (``[]``) immediately |
| 1682 |
after the name of the function, class, or type alias. The type parameters |
| 1683 |
are accessible within the scope of the generic object, but not elsewhere. |
| 1684 |
Thus, after a declaration ``def func[T](): pass``, the name ``T`` is not available in |
| 1685 |
the module scope. Below, the semantics of generic objects are described |
| 1686 |
with more precision. The scope of type parameters is modeled with a special |
| 1687 |
function (technically, an :ref:`annotation scope <annotation-scopes>`) that |
| 1688 |
wraps the creation of the generic object. |
| 1689 |
|
| 1690 |
Generic functions, classes, and type aliases have a |
| 1691 |
:attr:`~definition.__type_params__` attribute listing their type parameters. |
| 1692 |
|
| 1693 |
Type parameters come in three kinds: |
| 1694 |
|
| 1695 |
* :data:`typing.TypeVar`, introduced by a plain name (e.g., ``T``). Semantically, this |
| 1696 |
represents a single type to a type checker. |
| 1697 |
* :data:`typing.TypeVarTuple`, introduced by a name prefixed with a single |
| 1698 |
asterisk (e.g., ``*Ts``). Semantically, this stands for a tuple of any |
| 1699 |
number of types. |
| 1700 |
* :data:`typing.ParamSpec`, introduced by a name prefixed with two asterisks |
| 1701 |
(e.g., ``**P``). Semantically, this stands for the parameters of a callable. |
| 1702 |
|
| 1703 |
:data:`typing.TypeVar` declarations can define *bounds* and *constraints* with |
| 1704 |
a colon (``:``) followed by an expression. A single expression after the colon |
| 1705 |
indicates a bound (e.g. ``T: int``). Semantically, this means |
| 1706 |
that the :data:`!typing.TypeVar` can only represent types that are a subtype of |
| 1707 |
this bound. A parenthesized tuple of expressions after the colon indicates a |
| 1708 |
set of constraints (e.g. ``T: (str, bytes)``). Each member of the tuple should be a |
| 1709 |
type (again, this is not enforced at runtime). Constrained type variables can only |
| 1710 |
take on one of the types in the list of constraints. |
| 1711 |
|
| 1712 |
For :data:`!typing.TypeVar`\ s declared using the type parameter list syntax, |
| 1713 |
the bound and constraints are not evaluated when the generic object is created, |
| 1714 |
but only when the value is explicitly accessed through the attributes ``__bound__`` |
| 1715 |
and ``__constraints__``. To accomplish this, the bounds or constraints are |
| 1716 |
evaluated in a separate :ref:`annotation scope <annotation-scopes>`. |
| 1717 |
|
| 1718 |
:data:`typing.TypeVarTuple`\ s and :data:`typing.ParamSpec`\ s cannot have bounds |
| 1719 |
or constraints. |
| 1720 |
|
| 1721 |
All three flavors of type parameters can also have a *default value*, which is used |
| 1722 |
when the type parameter is not explicitly provided. This is added by appending |
| 1723 |
a single equals sign (``=``) followed by an expression. Like the bounds and |
| 1724 |
constraints of type variables, the default value is not evaluated when the |
| 1725 |
object is created, but only when the type parameter's ``__default__`` attribute |
| 1726 |
is accessed. To this end, the default value is evaluated in a separate |
| 1727 |
:ref:`annotation scope <annotation-scopes>`. If no default value is specified |
| 1728 |
for a type parameter, the ``__default__`` attribute is set to the special |
| 1729 |
sentinel object :data:`typing.NoDefault`. |
| 1730 |
|
| 1731 |
The following example indicates the full set of allowed type parameter declarations:: |
| 1732 |
|
| 1733 |
def overly_generic[ |
| 1734 |
SimpleTypeVar, |
| 1735 |
TypeVarWithDefault = int, |
| 1736 |
TypeVarWithBound: int, |
| 1737 |
TypeVarWithConstraints: (str, bytes), |
| 1738 |
*SimpleTypeVarTuple = (int, float), |
| 1739 |
**SimpleParamSpec = (str, bytearray), |
| 1740 |
]( |
| 1741 |
a: SimpleTypeVar, |
| 1742 |
b: TypeVarWithDefault, |
| 1743 |
c: TypeVarWithBound, |
| 1744 |
d: Callable[SimpleParamSpec, TypeVarWithConstraints], |
| 1745 |
*e: SimpleTypeVarTuple, |
| 1746 |
): ... |
| 1747 |
|
| 1748 |
.. _generic-functions: |
| 1749 |
|
| 1750 |
Generic functions |
| 1751 |
----------------- |
| 1752 |
|
| 1753 |
Generic functions are declared as follows:: |
| 1754 |
|
| 1755 |
def func[T](arg: T): ... |
| 1756 |
|
| 1757 |
This syntax is equivalent to:: |
| 1758 |
|
| 1759 |
annotation-def TYPE_PARAMS_OF_func(): |
| 1760 |
T = typing.TypeVar("T") |
| 1761 |
def func(arg: T): ... |
| 1762 |
func.__type_params__ = (T,) |
| 1763 |
return func |
| 1764 |
func = TYPE_PARAMS_OF_func() |
| 1765 |
|
| 1766 |
Here ``annotation-def`` indicates an :ref:`annotation scope <annotation-scopes>`, |
| 1767 |
which is not actually bound to any name at runtime. (One |
| 1768 |
other liberty is taken in the translation: the syntax does not go through |
| 1769 |
attribute access on the :mod:`typing` module, but creates an instance of |
| 1770 |
:data:`typing.TypeVar` directly.) |
| 1771 |
|
| 1772 |
The annotations of generic functions are evaluated within the annotation scope |
| 1773 |
used for declaring the type parameters, but the function's defaults and |
| 1774 |
decorators are not. |
| 1775 |
|
| 1776 |
The following example illustrates the scoping rules for these cases, |
| 1777 |
as well as for additional flavors of type parameters:: |
| 1778 |
|
| 1779 |
@decorator |
| 1780 |
def func[T: int, *Ts, **P](*args: *Ts, arg: Callable[P, T] = some_default): |
| 1781 |
... |
| 1782 |
|
| 1783 |
Except for the :ref:`lazy evaluation <lazy-evaluation>` of the |
| 1784 |
:class:`~typing.TypeVar` bound, this is equivalent to:: |
| 1785 |
|
| 1786 |
DEFAULT_OF_arg = some_default |
| 1787 |
|
| 1788 |
annotation-def TYPE_PARAMS_OF_func(): |
| 1789 |
|
| 1790 |
annotation-def BOUND_OF_T(): |
| 1791 |
return int |
| 1792 |
# In reality, BOUND_OF_T() is evaluated only on demand. |
| 1793 |
T = typing.TypeVar("T", bound=BOUND_OF_T()) |
| 1794 |
|
| 1795 |
Ts = typing.TypeVarTuple("Ts") |
| 1796 |
P = typing.ParamSpec("P") |
| 1797 |
|
| 1798 |
def func(*args: *Ts, arg: Callable[P, T] = DEFAULT_OF_arg): |
| 1799 |
... |
| 1800 |
|
| 1801 |
func.__type_params__ = (T, Ts, P) |
| 1802 |
return func |
| 1803 |
func = decorator(TYPE_PARAMS_OF_func()) |
| 1804 |
|
| 1805 |
The capitalized names like ``DEFAULT_OF_arg`` are not actually |
| 1806 |
bound at runtime. |
| 1807 |
|
| 1808 |
.. _generic-classes: |
| 1809 |
|
| 1810 |
Generic classes |
| 1811 |
--------------- |
| 1812 |
|
| 1813 |
Generic classes are declared as follows:: |
| 1814 |
|
| 1815 |
class Bag[T]: ... |
| 1816 |
|
| 1817 |
This syntax is equivalent to:: |
| 1818 |
|
| 1819 |
annotation-def TYPE_PARAMS_OF_Bag(): |
| 1820 |
T = typing.TypeVar("T") |
| 1821 |
class Bag(typing.Generic[T]): |
| 1822 |
__type_params__ = (T,) |
| 1823 |
... |
| 1824 |
return Bag |
| 1825 |
Bag = TYPE_PARAMS_OF_Bag() |
| 1826 |
|
| 1827 |
Here again ``annotation-def`` (not a real keyword) indicates an |
| 1828 |
:ref:`annotation scope <annotation-scopes>`, and the name |
| 1829 |
``TYPE_PARAMS_OF_Bag`` is not actually bound at runtime. |
| 1830 |
|
| 1831 |
Generic classes implicitly inherit from :data:`typing.Generic`. |
| 1832 |
The base classes and keyword arguments of generic classes are |
| 1833 |
evaluated within the type scope for the type parameters, |
| 1834 |
and decorators are evaluated outside that scope. This is illustrated |
| 1835 |
by this example:: |
| 1836 |
|
| 1837 |
@decorator |
| 1838 |
class Bag(Base[T], arg=T): ... |
| 1839 |
|
| 1840 |
This is equivalent to:: |
| 1841 |
|
| 1842 |
annotation-def TYPE_PARAMS_OF_Bag(): |
| 1843 |
T = typing.TypeVar("T") |
| 1844 |
class Bag(Base[T], typing.Generic[T], arg=T): |
| 1845 |
__type_params__ = (T,) |
| 1846 |
... |
| 1847 |
return Bag |
| 1848 |
Bag = decorator(TYPE_PARAMS_OF_Bag()) |
| 1849 |
|
| 1850 |
.. _generic-type-aliases: |
| 1851 |
|
| 1852 |
Generic type aliases |
| 1853 |
-------------------- |
| 1854 |
|
| 1855 |
The :keyword:`type` statement can also be used to create a generic type alias:: |
| 1856 |
|
| 1857 |
type ListOrSet[T] = list[T] | set[T] |
| 1858 |
|
| 1859 |
Except for the :ref:`lazy evaluation <lazy-evaluation>` of the value, |
| 1860 |
this is equivalent to:: |
| 1861 |
|
| 1862 |
annotation-def TYPE_PARAMS_OF_ListOrSet(): |
| 1863 |
T = typing.TypeVar("T") |
| 1864 |
|
| 1865 |
annotation-def VALUE_OF_ListOrSet(): |
| 1866 |
return list[T] | set[T] |
| 1867 |
# In reality, the value is lazily evaluated |
| 1868 |
return typing.TypeAliasType("ListOrSet", VALUE_OF_ListOrSet(), type_params=(T,)) |
| 1869 |
ListOrSet = TYPE_PARAMS_OF_ListOrSet() |
| 1870 |
|
| 1871 |
Here, ``annotation-def`` (not a real keyword) indicates an |
| 1872 |
:ref:`annotation scope <annotation-scopes>`. The capitalized names |
| 1873 |
like ``TYPE_PARAMS_OF_ListOrSet`` are not actually bound at runtime. |
| 1874 |
|
| 1875 |
.. _annotations: |
| 1876 |
|
| 1877 |
Annotations |
| 1878 |
=========== |
| 1879 |
|
| 1880 |
.. versionchanged:: 3.14 |
| 1881 |
Annotations are now lazily evaluated by default. |
| 1882 |
|
| 1883 |
Variables and function parameters may carry :term:`annotations <annotation>`, |
| 1884 |
created by adding a colon after the name, followed by an expression:: |
| 1885 |
|
| 1886 |
x: annotation = 1 |
| 1887 |
def f(param: annotation): ... |
| 1888 |
|
| 1889 |
Functions may also carry a return annotation following an arrow:: |
| 1890 |
|
| 1891 |
def f() -> annotation: ... |
| 1892 |
|
| 1893 |
Annotations are conventionally used for :term:`type hints <type hint>`, but this |
| 1894 |
is not enforced by the language, and in general annotations may contain arbitrary |
| 1895 |
expressions. The presence of annotations does not change the runtime semantics of |
| 1896 |
the code, except if some mechanism is used that introspects and uses the annotations |
| 1897 |
(such as :mod:`dataclasses` or :func:`functools.singledispatch`). |
| 1898 |
|
| 1899 |
By default, annotations are lazily evaluated in an :ref:`annotation scope <annotation-scopes>`. |
| 1900 |
This means that they are not evaluated when the code containing the annotation is evaluated. |
| 1901 |
Instead, the interpreter saves information that can be used to evaluate the annotation later |
| 1902 |
if requested. The :mod:`annotationlib` module provides tools for evaluating annotations. |
| 1903 |
|
| 1904 |
If the :ref:`future statement <future>` ``from __future__ import annotations`` is present, |
| 1905 |
all annotations are instead stored as strings:: |
| 1906 |
|
| 1907 |
>>> from __future__ import annotations |
| 1908 |
>>> def f(param: annotation): ... |
| 1909 |
>>> f.__annotations__ |
| 1910 |
{'param': 'annotation'} |
| 1911 |
|
| 1912 |
This future statement will be deprecated and removed in a future version of Python, |
| 1913 |
but not before Python 3.13 reaches its end of life (see :pep:`749`). |
| 1914 |
When it is used, introspection tools like |
| 1915 |
:func:`annotationlib.get_annotations` and :func:`typing.get_type_hints` are |
| 1916 |
less likely to be able to resolve annotations at runtime. |
| 1917 |
|
| 1918 |
|
| 1919 |
.. rubric:: Footnotes |
| 1920 |
|
| 1921 |
.. [#] The exception is propagated to the invocation stack unless |
| 1922 |
there is a :keyword:`finally` clause which happens to raise another |
| 1923 |
exception. That new exception causes the old one to be lost. |
| 1924 |
|
| 1925 |
.. [#] In pattern matching, a sequence is defined as one of the following: |
| 1926 |
|
| 1927 |
* a class that inherits from :class:`collections.abc.Sequence` |
| 1928 |
* a Python class that has been registered as :class:`collections.abc.Sequence` |
| 1929 |
* a builtin class that has its (CPython) :c:macro:`Py_TPFLAGS_SEQUENCE` bit set |
| 1930 |
* a class that inherits from any of the above |
| 1931 |
|
| 1932 |
The following standard library classes are sequences: |
| 1933 |
|
| 1934 |
* :class:`array.array` |
| 1935 |
* :class:`collections.deque` |
| 1936 |
* :class:`list` |
| 1937 |
* :class:`memoryview` |
| 1938 |
* :class:`range` |
| 1939 |
* :class:`tuple` |
| 1940 |
|
| 1941 |
.. note:: Subject values of type ``str``, ``bytes``, and ``bytearray`` |
| 1942 |
do not match sequence patterns. |
| 1943 |
|
| 1944 |
.. [#] In pattern matching, a mapping is defined as one of the following: |
| 1945 |
|
| 1946 |
* a class that inherits from :class:`collections.abc.Mapping` |
| 1947 |
* a Python class that has been registered as :class:`collections.abc.Mapping` |
| 1948 |
* a builtin class that has its (CPython) :c:macro:`Py_TPFLAGS_MAPPING` bit set |
| 1949 |
* a class that inherits from any of the above |
| 1950 |
|
| 1951 |
The standard library classes :class:`dict` and :class:`types.MappingProxyType` |
| 1952 |
are mappings. |
| 1953 |
|
| 1954 |
.. [#] A string literal appearing as the first statement in the function body is |
| 1955 |
transformed into the function's :attr:`~function.__doc__` attribute and |
| 1956 |
therefore the function's :term:`docstring`. |
| 1957 |
|
| 1958 |
.. [#] A string literal appearing as the first statement in the class body is |
| 1959 |
transformed into the namespace's :attr:`~type.__doc__` item and therefore |
| 1960 |
the class's :term:`docstring`. |