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